表未在SQLite数据库中创建

时间:2014-11-05 07:59:12

标签: android sqlite

我试图使用SQLite在数据库中插入值。

这是我的适配器代码:

public class ReminderDatabaseAdapter {
 public static final String MYDATABASE_NAME = "REMINDER_DATABASE";
 public static final String MYDATABASE_TABLE = "REMINDER_TABLE";
 public static final int MYDATABASE_VERSION = 1;
 public static final String KEY_ID = "_id";
 public static final String USERNAME = "USERNAME";
 public static final String REMINDER_TYPE = "REMINDER_TYPE";
 public static final String ASSIGNMENT_STARTTIME = "ASSIGNMENT_STARTTIME";
 public static final String ASSIGNMENT_ENDTIME = "ASSIGNMENT_ENDTIME";
 public static final String REMINDER_DATE = "REMINDER_DATE";
 public static final String REMINDER_TIME = "REMINDER_TIME";
 public static final String DESCRIPTION = "DESCRIPTION";
 private ReminderDataBaseHelper sqLiteHelper;
 private SQLiteDatabase sqLiteDatabase;
 private Context context;

 public ReminderDatabaseAdapter(Context c){
      context = c;
     }
 public ReminderDatabaseAdapter openToRead() throws android.database.SQLException {
          sqLiteHelper = new ReminderDataBaseHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
          sqLiteDatabase = sqLiteHelper.getReadableDatabase();
          return this; 
     }       
    public ReminderDatabaseAdapter openToWrite() throws android.database.SQLException {
          sqLiteHelper = new ReminderDataBaseHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
          sqLiteDatabase = sqLiteHelper.getWritableDatabase();
          return this; 
    }    
    public void close(){
         sqLiteHelper.close();
    }

    public int insertEntry(String userName,String reminderType,String assignmentStart,String assignmentEnd,String reminderDate,String reminderTime,String desription){
        ContentValues newValues = new ContentValues();
        newValues.put("USERNAME", userName);
        newValues.put("REMINDER_TYPE", reminderType);
        newValues.put("ASSIGNMENT_STARTTIME", assignmentStart);
        newValues.put("ASSIGNMENT_ENDTIME",assignmentEnd);
        newValues.put("REMINDER_DATE",reminderDate);
        newValues.put("REMINDER_TIME",reminderTime );
        newValues.put("DESCRIPTION", desription);

        Cursor cursor=sqLiteDatabase.query(MYDATABASE_TABLE, null, " USERNAME=? AND REMINDER_DATE=? AND REMINDER_TIME=?", new String[]{userName,reminderDate,reminderTime}, null, null, null);
        if(cursor.getCount()<1) 
        {
            cursor.close();
            sqLiteDatabase.insert(MYDATABASE_TABLE, null, newValues);
            Toast.makeText(context, "REMINDER INSERTED", Toast.LENGTH_LONG).show();
            return 1;
        }
        else{
            Toast.makeText(context, "REMINDER WITH CURRENT DATE AND TIME EXISTS", Toast.LENGTH_LONG).show();
            return 0;
        }   
    }

}

对于数据库助手,我有以下代码:

public class ReminderDataBaseHelper extends SQLiteOpenHelper {
 public static final String MYDATABASE_NAME = "REMINDER_DATABASE";
 public static final String MYDATABASE_TABLE = "REMINDER_TABLE";
 public static final int MYDATABASE_VERSION = 1;
 public static final String KEY_ID = "_id";
 public static final String USERNAME = "USERNAME";
 public static final String REMINDER_TYPE = "REMINDER_TYPE";
 public static final String ASSIGNMENT_STARTTIME = "ASSIGNMENT_STARTTIME";
 public static final String ASSIGNMENT_ENDTIME = "ASSIGNMENT_ENDTIME";
 public static final String REMINDER_DATE = "REMINDER_DATE";
 public static final String REMINDER_TIME = "REMINDER_TIME";
 public static final String DESCRIPTION = "DESCRIPTION";

 private static final String SCRIPT_CREATE_DATABASE =
          "create table " + MYDATABASE_TABLE + " ("
          + KEY_ID + " integer primary key autoincrement, "
          + USERNAME + " text not null, "
          + REMINDER_TYPE + " text not null, "
          + ASSIGNMENT_STARTTIME + " text not null, "
          + ASSIGNMENT_ENDTIME + " text not null, "
          + REMINDER_DATE + " text not null, "
          + REMINDER_TIME + " text not null, "
          + DESCRIPTION + " text not null);";
  public ReminderDataBaseHelper(Context context, String name,
    CursorFactory factory, int version) {
      super(context, name, factory, version);
  }
  public void onCreate(SQLiteDatabase db) {
      db.execSQL(SCRIPT_CREATE_DATABASE);
  }
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE "+MYDATABASE_TABLE+";");
    onCreate(db);
  }
 }

但我不知道为什么app会崩溃。 这是logcat:

11-05 13:51:28.463: E/AndroidRuntime(26780): FATAL EXCEPTION: main
11-05 13:51:28.463: E/AndroidRuntime(26780): Process: com.example.loginphase, PID: 26780
11-05 13:51:28.463: E/AndroidRuntime(26780): android.database.sqlite.SQLiteException: no such table: REMINDER_TABLE (code 1): , while compiling: SELECT * FROM REMINDER_TABLE WHERE  USERNAME=? AND REMINDER_DATE=? AND REMINDER_TIME=?
11-05 13:51:28.463: E/AndroidRuntime(26780):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
11-05 13:51:28.463: E/AndroidRuntime(26780):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
11-05 13:51:28.463: E/AndroidRuntime(26780):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
11-05 13:51:28.463: E/AndroidRuntime(26780):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
11-05 13:51:28.463: E/AndroidRuntime(26780):    at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58)
11-05 13:51:28.463: E/AndroidRuntime(26780):    at android.database.sqlite.SQLiteQuery.(SQLiteQuery.java:37)
11-05 13:51:28.463: E/AndroidRuntime(26780):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
11-05 13:51:28.463: E/AndroidRuntime(26780):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
11-05 13:51:28.463: E/AndroidRuntime(26780):    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
11-05 13:51:28.463: E/AndroidRuntime(26780):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)

在Fragment中我正在使用它:

private ReminderDatabaseAdapter mySQLiteAdapter;
mySQLiteAdapter = new ReminderDatabaseAdapter(rootView.getContext());
mySQLiteAdapter.openToWrite();        mySQLiteAdapter.insertEntry(name,ReminderType,assignmentBegin,assignmentEnd,reminderdate,remindertime ,descriptionDetails);

1 个答案:

答案 0 :(得分:0)

我唯一看到的是你应该已经在你的应用程序数据文件夹中拥有数据库文件,版本1不包含REMINDER_TABLE(在第一次运行时创建)。这种错误通常发生在开发时间

eighter unistall并安装应用程序,以便调用onCreate of database helper。或者将db版本增加到2并尝试(MYDATABASE_VERSION = 2;)以便调用onUpgrade。