SQLiteOpenHelper
上课
public class DataDB extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "Datas";
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + FeedEntry.TABLE_NAME + " (" +
FeedEntry.COLUMN_NAME_ID + " INTEGER PRIMARY KEY," +
FeedEntry.COLUMN_NAME_NAME + " TEXT," +
FeedEntry.COLUMN_NAME_DESC + " TEXT," +
FeedEntry.COLUMN_NAME_IMG_URL + " TEXT," +
FeedEntry.COLUMN_NAME_PRICE + " TEXT);";
private static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + FeedEntry.TABLE_NAME;
public DataDB(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
Log.e("Syntax",SQL_CREATE_ENTRIES);
sqLiteDatabase.execSQL(SQL_CREATE_ENTRIES);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL(SQL_DELETE_ENTRIES);
onCreate(sqLiteDatabase);
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
}
插入内容的代码
ContentValues values = new ContentValues();
values.put(FeedEntry.COLUMN_NAME_ID, Integer.parseInt(oneObject.getString("id")));
values.put(FeedEntry.COLUMN_NAME_NAME, oneObject.getString("name"));
values.put(FeedEntry.COLUMN_NAME_DESC, oneObject.getString("desc"));
values.put(FeedEntry.COLUMN_NAME_IMG_URL, oneObject.getString("img_url"));
values.put(FeedEntry.COLUMN_NAME_PRICE, oneObject.getString("price"));
db.insert(FeedEntry.TABLE_NAME, null, values);
错误消息
10-12 23:20:05.244 19171-19171/com.dkartindia.dkart E/SQLiteLog: (1) no such table: category
10-12 23:20:05.250 19171-19171/com.dkartindia.dkart E/SQLiteDatabase: Error inserting name=VEGETABLES0 price=20.00 description=King prawns are the most popular species of prawn in Australia, due no doubt to their rich flavour and moist flesh. img_url=http://www.dkartindia.com/image.jpg id=0
android.database.sqlite.SQLiteException: no such table: category (code 1): , while compiling: INSERT INTO category(name,price,description,img_url,id) VALUES (?,?,?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:898)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:509)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1499)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1371)
at com.dkartindia.dkart.Activities.Home$2.onResponse(Home.java:114)
at com.dkartindia.dkart.Activities.Home$2.onResponse(Home.java:85)
at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5717)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
答案 0 :(得分:1)
问题在于我的代码中有Drop Command
。这就是为什么创建表后它会丢弃的原因。通过删除线条解决了我的问题。