2014-11-05 238 views
-2

我試圖使用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); 
    } 
} 

但我不知道爲什麼應用程序崩潰。 這裏是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) 

而在片段正在使用它像這樣:

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

您是否嘗試卸載應用程序或更改版本號? – Nabin 2014-11-05 08:03:01

+0

做@Nabin說的話,並且檢查日誌是否有創建表的錯誤。 – zozelfelfo 2014-11-05 08:07:49

+0

@Nabin是的,我沒有嘗試 – Smu 2014-11-05 08:07:55

回答

0

唯一的事情是,你應該已經有版本1的應用程式資料夾中的數據庫文件它不包含REMINDER_TABLE(在第一次運行時創建)。這種錯誤通常發生在開發期間

eighter unistall並istall該應用程序,以便數據庫幫助程序的onCreate被調用。或者將db版本增加到2並嘗試(MYDATABASE_VERSION = 2;)以便調用onUpgrade。