2012-04-25 90 views
1

我不確定使用我的簡單程序學習如何使用SQLite數據庫。我將我的程序從this site上找到的程序中刪除。我決定刪除使用變量的問題,並將所有內容寫入我的Create table字符串中。我也試圖用相應的KEY_CONTENT等來做到這一點無濟於事。希望這是足夠的代碼讓你看到什麼是錯的。另外,有沒有我可以編寫的任何代碼(日誌或吐司輸出),這將有助於弄清楚什麼是錯誤的?Android SQLite數據庫:麻煩創建表格並插入

public class SQLiteAdapter { 

public static final String DB_NAME = "My_Database"; 
public static final String DB_TABLE = "My_Table"; 
public static final int DB_VERSION = 1; 
public static final String KEY_CONTENT = "My_Content"; 


String SCRIPT_CREATE_DATABASE = "CREATE TABLE My_Table (_id INTEGER PRIMARY KEY AUTOINCREMENT, My_Content TEXT);"; 

private SQLiteHelper sqLiteHelper; 
private SQLiteDatabase sqLiteDatabase; 

private Context context; 

public SQLiteAdapter(Context c){ 
    context = c; 
} 
public SQLiteAdapter openToRead() throws android.database.SQLException { 
     sqLiteHelper = new SQLiteHelper(context, DB_NAME, null, DB_VERSION); 
     sqLiteDatabase = sqLiteHelper.getReadableDatabase(); 
     return this; 
    } 
public SQLiteAdapter openToWrite() throws android.database.SQLException { 
     sqLiteHelper = new SQLiteHelper(context, DB_NAME, null, DB_VERSION); 
     sqLiteDatabase = sqLiteHelper.getWritableDatabase(); 
     return this; 
    } 

public void close(){ 
     sqLiteHelper.close(); 
    } 

public long insert(String content){ 

     ContentValues contentValues = new ContentValues(); 
     contentValues.put("My_Content", content); 
     return sqLiteDatabase.insert("My_Table", null, contentValues); 
    } 
public int deleteAll(){ 
     return sqLiteDatabase.delete(DB_TABLE, null, null); 
    } 

    public String queueAll(){ 
     String[] columns = new String[]{KEY_CONTENT}; 
     Cursor cursor = sqLiteDatabase.query(DB_TABLE, columns, 
     null, null, null, null, null); 
     String result = ""; 

     int index_CONTENT = cursor.getColumnIndex(KEY_CONTENT); 
     for(cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()){ 
     result = result + cursor.getString(index_CONTENT) + "\n"; 
     } 

     return result; 
    } 

    public class SQLiteHelper extends SQLiteOpenHelper { 

     public SQLiteHelper(Context context, String name, 
     CursorFactory factory, int version) { 
     super(context, name, factory, version); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 

     db.execSQL(SCRIPT_CREATE_DATABASE); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 

     } 

    } 

}

我的主要活動開始處理SQLite的DB這樣的:

mySQLiteAdapter = new SQLiteAdapter(this); 
     mySQLiteAdapter.openToWrite(); 
     mySQLiteAdapter.insert("First try"); 

這裏是logcat的:

04-25 14:19:37.404: E/Database(217): Error inserting My_Content=First try 
04-25 14:19:37.404: E/Database(217): android.database.sqlite.SQLiteException: no such table: My_Table: , while compiling: INSERT INTO My_Table(My_Content) VALUES(?); 
04-25 14:19:37.404: E/Database(217): at android.database.sqlite.SQLiteProgram.native_compile(Native Method) 
04-25 14:19:37.404: E/Database(217): at android.database.sqlite.SQLiteProgram.compile(SQLiteProgram.java:110) 
04-25 14:19:37.404: E/Database(217): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59) 
04-25 14:19:37.404: E/Database(217): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41) 
04-25 14:19:37.404: E/Database(217): at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1027) 
04-25 14:19:37.404: E/Database(217): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1413) 
04-25 14:19:37.404: E/Database(217): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1286) 
04-25 14:19:37.404: E/Database(217): at com.willmer.trialsqlite.SQLiteAdapter.insert(SQLiteAdapter.java:53) 
04-25 14:19:37.404: E/Database(217): at com.willmer.trialsqlite.TrialSQLiteActivity.onCreate(TrialSQLiteActivity.java:25) 
04-25 14:19:37.404: E/Database(217): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
04-25 14:19:37.404: E/Database(217): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 
04-25 14:19:37.404: E/Database(217): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 
04-25 14:19:37.404: E/Database(217): at android.app.ActivityThread.access$2200(ActivityThread.java:119) 
04-25 14:19:37.404: E/Database(217): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 
04-25 14:19:37.404: E/Database(217): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-25 14:19:37.404: E/Database(217): at android.os.Looper.loop(Looper.java:123) 
04-25 14:19:37.404: E/Database(217): at android.app.ActivityThread.main(ActivityThread.java:4363) 
04-25 14:19:37.404: E/Database(217): at java.lang.reflect.Method.invokeNative(Native Method) 
04-25 14:19:37.404: E/Database(217): at java.lang.reflect.Method.invoke(Method.java:521) 
04-25 14:19:37.404: E/Database(217): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
04-25 14:19:37.404: E/Database(217): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
04-25 14:19:37.404: E/Database(217): at dalvik.system.NativeStart.main(Native Method) 
+1

我建議你不要調用任何數據庫表'表' - 這是一個保留字。 – GregHNZ 2012-04-25 05:04:21

回答

1

將您的代碼替換爲以下代碼。 因爲Table本身是一個關鍵字。

String SCRIPT_CREATE_DATABASE = "CREATE TABLE My_Table (_id INTEGER PRIMARY KEY AUTOINCREMENT, My_Content TEXT);"; 
0

你必須給間距「表格」,「(」和「值」,「(」。

like below

INSERT INTO Table (Content) VALUES (?); 
+0

我有點困惑。如何在使用sqLiteDatabase.insert(「Table」,null,contentValues)時添加空格;即使我將所有Table和Content的內容更改爲My_Table和My_Content,也是如此。 – willmer 2012-04-25 05:17:22