2013-08-18 72 views
1

logcat說thare是數據庫類中的單詞「table」附近的SQLite錯誤。我查看了這段代碼,並沒有看到任何語法錯誤,我錯過了什麼?創建表方法中的SQLite語法錯誤

數據庫類

public class Database { 

public static final String DATABASE = "tester"; 

public static final int DATABASE_VERSION = 1; 

public static final String TABLENAME = "table"; 

public static final String _ID = "_id"; 

// collumns 
public static final String SAMPLE = "sample"; 

private static final String SCRIPT_CREATE_TABLE = 
     "CREATE TABLE IF NOT EXISTS " + TABLENAME + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
     SAMPLE + " TEXT)"; 


      SQLiteDatabase sqLiteDatabase; 
      SQLiteHelper sqLiteHelper; 
      Context context; 

      public Database(Context c){ 
        context = c; 
       } 

      public void openToRead() throws android.database.SQLException { 
        sqLiteHelper = new SQLiteHelper(context, DATABASE, null, DATABASE_VERSION); 
        sqLiteDatabase = sqLiteHelper.getReadableDatabase(); 
       } 

      public void openToWrite() throws android.database.SQLException { 
        sqLiteHelper = new SQLiteHelper(context, DATABASE, null, DATABASE_VERSION); 
        sqLiteDatabase = sqLiteHelper.getWritableDatabase(); 
       } 

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

       public void deleteDB(){ 
        context.deleteDatabase(DATABASE); 
       } 

        public void insert(){ 

         ContentValues contentValues = new ContentValues(); 
         contentValues.put(SAMPLE, "TEST ONE"); 
         sqLiteDatabase.insert(TABLENAME, null, contentValues); 


        } 


        public String get(){ 
         String returnString = ""; 
         Cursor cursor = sqLiteDatabase.query(TABLENAME, new String[]{SAMPLE}, null, null, null, null, null); 
         if(cursor!=null){ 
          cursor.moveToFirst(); 
           returnString = cursor.getString(cursor.getColumnIndex(SAMPLE)); 

         } 
         return returnString; 
        } 



     public class SQLiteHelper extends SQLiteOpenHelper { 

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

        // onCreate of the SQLiteOpenhelper only called if the database does not already exist 
        @Override 
        public void onCreate(SQLiteDatabase db) { 

        db.execSQL(SCRIPT_CREATE_TABLE); 


        } 

        @Override 
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
        db.execSQL("DROP TABLE IF EXISTS " + SCRIPT_CREATE_TABLE); 



        onCreate(db); 
        } 

       } 

} 

MainAcivity類

public class MainActivity extends Activity { 
TextView textViewOne; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    textViewOne = (TextView) findViewById(R.id.textView1); 


    Database db = new Database(this); 
    db.openToWrite(); 
    db.insert(); 
    db.close(); 

    db.openToRead(); 
    String getStr = db.get(); 
    db.close(); 

    textViewOne.setText(getStr); 

} 



} 
+0

它可能是'table'是關鍵字。所以在創建表時,可能會出錯。嘗試選擇一個不同的表名,如「MyTable」 –

回答

3

你得表名 「表」 - 這就是問題所在,因爲它是SQLite's keyword

如果你想創建具有該名稱的表,你應該引用它象下面這樣:

CREATE TABLE IF NOT EXISTS "table" ... 
2

你想創建一個表,而不引用名字叫table(SQLite中一個RESERVERD字)。在sqlite3提示符下做同樣的事情;

sqlite> CREATE TABLE IF NOT EXISTS table (_id INTEGER PRIMARY KEY AUTOINCREMENT, 
              sample TEXT); 
Error: near "table": syntax error 

如果你真的意味着有一個名爲「表」的表,你就需要引用的名稱;

sqlite> CREATE TABLE IF NOT EXISTS "table" (_id INTEGER PRIMARY KEY AUTOINCREMENT, 
              sample TEXT); 
sqlite>