gpt4 book ai didi

Android SQLite建表语句

转载 作者:行者123 更新时间:2023-11-29 23:19:12 25 4
gpt4 key购买 nike

我遇到一个问题,这个表根本没有在 android 中创建。
这是整个 MyDBHandler:

package com.example.plrardiakao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDBHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "MudeDB.db";
public static final String TABLE_ITEMS = "Items";

//helper for types
public static final String VARCHAR_TYPE = " VARCHAR(50)";
public static final String BOOL_TYPE = " BOOLEAN";
public static final String INT_TYPE = " INTEGER";

//helper for column names
public static final String COLUMN_ENTRY_ID = "id"; //integer
public static final String COLUMN_TITLE = "itemtitle"; //varchar(50)
public static final String COLUMN_AUTHOR = "author"; //varchar(50)
public static final String COLUMN_CATEGORY = "category"; //varchar(50)
public static final String COLUMN_DATE = "date"; //int
public static final String COLUMN_TYPE = "type"; //varchar(50)
public static final String COLUMN_COUNTRY = "country"; //varchar(50)
public static final String COLUMN_COLOUR = "colour"; //varchar(50)
public static final String COLUMN_MATERIAL = "material"; //varchar(50)
public static final String COLUMN_FAVOURITE = "is_favourite"; //boolean
public static final String COLUMN_IMGRES = "imgres"; //varchar(50)
public static final String COLUMN_NUMBER_OF_PICS = "nr_of_pics"; // integer

public MyDBHandler(Context context, String name,
CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_ITEMS_TABLE = "CREATE TABLE " + TABLE_ITEMS +
"("
+ COLUMN_ENTRY_ID + INT_TYPE +" PRIMARY KEY AUTOINCREMENT,"
+ COLUMN_TITLE + VARCHAR_TYPE + ","
+ COLUMN_AUTHOR + VARCHAR_TYPE + ","
+ COLUMN_CATEGORY + VARCHAR_TYPE + ","
+ COLUMN_DATE + INT_TYPE + ","
+ COLUMN_TYPE + VARCHAR_TYPE + ","
+ COLUMN_COUNTRY + VARCHAR_TYPE + ","
+ COLUMN_COLOUR + VARCHAR_TYPE + ","
+ COLUMN_MATERIAL + VARCHAR_TYPE + ","
+ COLUMN_FAVOURITE + BOOL_TYPE + ","
+ COLUMN_IMGRES + VARCHAR_TYPE + ","
+ COLUMN_NUMBER_OF_PICS + INT_TYPE +
")";
db.execSQL(CREATE_ITEMS_TABLE);
}
//CREATE TABLE Items(id INTEGER PRIMARY KEY AUTOINCREMENT, itemtitle VARCHAR(50),author)
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ITEMS);
onCreate(db);
}

public void addItem(Item item) {

ContentValues values = new ContentValues();
values.put(COLUMN_TITLE, item.getItemTitle());
values.put(COLUMN_AUTHOR, item.getAuthor());
values.put(COLUMN_CATEGORY, item.getCategory());
values.put(COLUMN_DATE, item.getDate());
values.put(COLUMN_TYPE, item.getType());
values.put(COLUMN_COUNTRY, item.getCountry());
values.put(COLUMN_COLOUR, item.getColour());
values.put(COLUMN_MATERIAL, item.getMaterial());
values.put(COLUMN_FAVOURITE, item.getFavourite());
values.put(COLUMN_IMGRES, item.getImgres());
values.put(COLUMN_NUMBER_OF_PICS, item.getNumberOfPics());

SQLiteDatabase db = this.getWritableDatabase();

db.insert(TABLE_ITEMS, null, values);
db.close();

}

public int findPictureNumber(String itemtitle) {
String query = "SELECT nr_of_pics FROM " + TABLE_ITEMS + " WHERE " +COLUMN_TITLE + "=" + "'"+itemtitle+"'" ;
SQLiteDatabase db = this.getWritableDatabase();

Cursor cursor = db.rawQuery(query, null);


int PicsNumber=0;

if (cursor.moveToFirst()) {
cursor.moveToFirst();
PicsNumber=Integer.parseInt(cursor.getString(0));
cursor.close();
return PicsNumber;
} else {
//wtf?
}
db.close();
return 0;
}

public void changeFavourite(String itemtitle, boolean isFavourite)
{

SQLiteDatabase db = this.getWritableDatabase();
String strSQL = "UPDATE "+TABLE_ITEMS+" SET "+COLUMN_FAVOURITE +"="+ isFavourite +" WHERE columnId = "+ "'"+itemtitle+"'";

db.execSQL(strSQL);

}

public String AuthorName(String itemtitle) {
String query = "SELECT " +COLUMN_AUTHOR+ " FROM " + TABLE_ITEMS + " WHERE " +COLUMN_TITLE + "=" + "'"+itemtitle+"'" ;

SQLiteDatabase db = this.getWritableDatabase();

Cursor cursor = db.rawQuery(query, null);

String authorname="error1";

if (cursor.moveToFirst()) {
cursor.moveToFirst();
authorname=cursor.getString(0);
cursor.close();
} else {
//wtf?
}
db.close();
return authorname;
}

}

有一个错误,可能是在 Create_table 的某个地方,但我不知道在哪里。我知道该表未创建,之前的占位符表存在,并且在执行时显示错误,某些列不存在。怎么了?

最佳答案

为什么你没有发布错误?你可以发布吗?另外,如果您在数据库表中进行了更改,那么您必须增加数据库版本

关于Android SQLite建表语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27473261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com