原因:android.database.sqlite.SQLiteException:无法将只读数据库从版本0升级到1

前端之家收集整理的这篇文章主要介绍了原因:android.database.sqlite.SQLiteException:无法将只读数据库从版本0升级到1前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
尝试创建sqlite数据库时,我收到以下LogCat异常:

Caused by: android.database.sqlite.sqliteException: Can’t upgrade read-only database from version 0 to 1

这是我的代码

  1. import static android.provider.BaseColumns._ID;
  2. import static org.inetizen.university_tiMetable.Constants.DAY_OF_WEEK;
  3. import static org.inetizen.university_tiMetable.Constants.DURATION;
  4. import static org.inetizen.university_tiMetable.Constants.MODULE_CODE;
  5. import static org.inetizen.university_tiMetable.Constants.ROOM;
  6. import static org.inetizen.university_tiMetable.Constants.START_TIME;
  7. import static org.inetizen.university_tiMetable.Constants.TABLE_NAME;
  8. import static org.inetizen.university_tiMetable.Constants.TYPE_OF_SESSION;
  9. import android.content.Context;
  10. import android.database.sqlite.sqliteDatabase;
  11. import android.database.sqlite.sqliteOpenHelper;
  12.  
  13. public class TiMetableData extends sqliteOpenHelper {
  14. private static final String DATABASE_NAME = "tiMetable.db";
  15. private static final int DATABASE_VERSION = 1;
  16.  
  17. /** Create a helper object for the TiMetable database **/
  18. public TiMetableData(Context ctx) {
  19. super(ctx,DATABASE_NAME,null,DATABASE_VERSION);
  20. }
  21.  
  22. @Override
  23. public void onCreate(sqliteDatabase db) {
  24. db.execsql("CREATE TABLE " + TABLE_NAME + " (" +_ID
  25. + " INTEGER PRIMARY KEY AUTOINCREMENT " + MODULE_CODE
  26. + " TEXT NOT NULL " + DAY_OF_WEEK + " TEXT NOT NULL "
  27. + START_TIME + " INTEGER NOT NULL " + DURATION
  28. + " INTEGER NOT NULL " + TYPE_OF_SESSION + " TEXT NOT NULL "
  29. + ROOM + " TEXT NOT NULL )");
  30. }
  31.  
  32. @Override
  33. public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {
  34. db.execsql("DROP TABLE IF EXISTS " + TABLE_NAME);
  35. onCreate(db);
  36. }
  37. }

这是我的LogCat:

06001

有关如何解决异常的任何建议?

解决方法

您使用的sql语法看起来不正确,您缺少逗号.试试这个: db.execsql(“CREATE TABLE”TABLE_NAME“(”_ ID“INTEGER PRIMARY KEY AUTOINCREMENT,”MODULE_CODE“TEXT NOT NULL,”DAY_OF_WEEK“TEXT NOT NULL,”START_TIME“INTEGER NOT NULL,”DURATION“INTEGER NOT NULL,”TYPE_OF_SESSION“ TEXT NOT NULL,“ROOM”TEXT NOT NULL)“);

猜你在找的Android相关文章