Android SQLite数据库:创建表和插入时出现问题

时间:2012-04-25 04:57:49

标签: android sqlite

我不确定我的简单程序是如何学习如何使用SQLite数据库的。我的程序基于this site上找到的程序。我决定删除使用变量的问题,并将所有内容写入我的Create表字符串中。我也尝试用适当的KEY_CONTENT等来做这件事无济于事。希望这是足够的代码,你可以看到什么是错的。此外,是否有任何我可以编写的代码(日志或Toast输出),这将有助于找出什么是错的?

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)

2 个答案:

答案 0 :(得分:1)

使用以下代码替换您的代码。 因为Table本身就是关键字。

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

答案 1 :(得分:0)

你必须在“表”,“(”和“值”,“(”。

)之间留出空格

如下所示

INSERT INTO Table (Content) VALUES (?);