sqlite插入数据库并更新数据失败

程序猿大波 2018-04-20 10:33:38
public class MySqlDataHelper extends SQLiteOpenHelper {
//上下文的context
private Context mContext;

//PS:数据库的标准写法,可以直接复制使用并稍微修改
//创建一个书的表
public static final String CREATE_BOOK = "create table book (" + "id integer primary key autoincrement," + "author text," + "price real," + "pages integer," + "name text)";

//创建一个分类的表
public static final String CREATE_CATEGORY = "create table Category (" + "id integer primary key autoincrement," + "category_name text," + "category_code integer)";

public MySqlDataHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
mContext = context;
}

/**
* 这里是创建
* @param sqLiteDatabase
*/
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
//SQLiteDatabase中的execSQL方法来执行建表语句
//sqLiteDatabase.execSQL(CREATE_BOOK);
//弹出提示创建完成
Toast.makeText(mContext, "Create succeeded", Toast.LENGTH_SHORT).show();
}

/**
* 这里是更新
* @param sqLiteDatabase
* @param i
* @param i1
*/
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("drop table if exists Book");
sqLiteDatabase.execSQL("drop table if exists Category");
onCreate(sqLiteDatabase);
}
}
这一段代码运行应该没问题,提示“create succeeded”,但是后面插入数据出现问题
helper = new MySqlDataHelper(this,"BookStore.db",null,2);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
helper.getWritableDatabase();
}
});
Button add_data = (Button)findViewById(R.id.add_data);
add_data.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
// 开始组装第一条数据
values.put("name", "The Da Vinci Code");
values.put("author", "Dan Brown");
values.put("pages", 454);
values.put("price", 16.96);
db.insert("Book", null, values); // 插入第一条数据
values.clear();
// 开始组装第二条数据
values.put("name", "The Lost Symbol");
values.put("author", "Dan Brown");
values.put("pages", 510);
values.put("price", 19.95);
db.insert("Book", null, values); // 插入第二条数据
}
});
错误提示:
E/SQLiteLog: (1) no such table: Book
E/SQLiteDatabase: Error inserting name=The Da Vinci Code price=16.96 pages=454 author=Dan Brown
android.database.sqlite.SQLiteException: no such table: Book (code 1): , while compiling: INSERT INTO Book(name,price,pages,author) VALUES (?,?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:897)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:508)
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:1470)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1343)
at com.example.a47136.sqlite_test.MainActivity$2.onClick(MainActivity.java:38)
at android.view.View.performClick(View.java:5661)
at android.view.View$PerformClick.run(View.java:22380)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6267)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:920)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
...全文
1830 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
程序猿大波 2018-04-23
  • 打赏
  • 举报
回复
@开发者_android 但是提示成功建表呀,又说no such table: Book
开发者_android 2018-04-20
  • 打赏
  • 举报
回复
表不存在 1.建表失败。 2.表名用错。
开发者_android 2018-04-20
  • 打赏
  • 举报
回复
android.database.sqlite.SQLiteException: no such table: Book (code 1): , while compiling: INSERT INTO Book(name,price,pages,author) VALUES (?,?,?,?) 有什么不明白?数据往DB的表中插入,连这个表都没有,你往哪插数据?
程序猿大波 2018-04-20
  • 打赏
  • 举报
回复
@开发者_android 不明白
程序猿大波 2018-04-20
  • 打赏
  • 举报
回复
不明白,能详细说一下吗

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧