create table方法中的SQLite语法错误

时间:2013-08-18 10:06:03

标签: android database sqlite

logcat说thare是这里数据库类中“table”一词附近的SQLite错误。我查看了这段代码,我没有看到任何ayntax错误,我错过了什么?

数据库类

 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);

}



 }

2 个答案:

答案 0 :(得分:3)

你有一个名为“table”的表 - 这就是问题,因为它是SQLite's keyword

如果您想创建具有该名称的表格,您应该如下所示引用它:

CREATE TABLE IF NOT EXISTS "table" ...

答案 1 :(得分:2)

您正在尝试创建一个名为table的表(SQLite中的一个保留字)而不引用该名称。在sqlite3提示符处执行相同的操作会给出;

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

如果确实意味着要有一个名为“table”的表,则需要引用该名称;

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