SQLite错误附近:附近" TABLEbook":语法错误

时间:2014-09-28 18:11:12

标签: android sql sqlite

在我的情况下,我想在数据库中添加一些数据,但它一直在告诉

   09-28 17:30:42.955: E/SQLiteLog(8370): (1) near "TABLEbook": syntax error

但我无法发现任何错误。 请帮忙...... !!

这是我的ActivityMain类

public class MainActivity extends ActionBarActivity implements OnClickListener {
Button save;
EditText name,category,auther,qnt;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);                   
    save=(Button)findViewById(R.id.btn_add);
    name=(EditText)findViewById(R.id.et_name);
    category=(EditText)findViewById(R.id.et_cat);
    auther=(EditText)findViewById(R.id.et_auth);
    qnt=(EditText)findViewById(R.id.et_qn);
    save.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {

    DatabaseHelper db = new DatabaseHelper(this);

        String _name= name.getText().toString();
        String _cat= category.getText().toString();
        String _auth= auther.getText().toString();
        String _qnt=qnt.getText().toString();

        Log.d("Insert:","Inserting...");
        db.addAllDatabase(new Book(_name,_cat,_auth,_qnt));

}
}

这是我的书类

public class Book {

int _id;
String _name;
String _cat;
String _auth;
String _qunt;

public Book(){}

public Book(int id,String name,String cat, String auth, String qunt){
    this._id = id;
    this._name = name;
    this._cat = cat;
    this._auth = auth;
    this._qunt = qunt;
}
public Book(String name,String cat, String auth, String qunt){
    this._name = name;
    this._cat = cat;
    this._auth = auth;
    this._qunt = qunt;
}

public int getID() {
    return _id;
}

public void setID(int id) {
    this._id = id;
}

public String getName() {
    return _name;
}

public void setName(String name) {
    this._name = name;
}

public String getCat() {
    return _cat;
}

public void setCat(String cat) {
    this._cat = cat;
}

public String getAuth() {
    return _auth;
}

public void setAuth(String auth) {
    this._auth = auth;
}

public String getQunt() {
    return _qunt;
}

public void setQunt(String qunt) {
    this._qunt = qunt;
}

}

这是我的DAtabaseHelper Class

public class DatabaseHelper extends SQLiteOpenHelper {
//constant variables
private static final int DATABASE_VERSION = 8;
private static final String DATABASE_NAME = "bookManager";
private static final String TABLE_BOOK = "book";

private static final String KEY_ID="id";
private static final String KEY_NAME= "name";
private static final String KEY_CATEGORY = "category";
private static final String KEY_AUTHER = "auther";
private static final String KEY_QUNTITY = "quntity";

public DatabaseHelper(Context context){
    super(context,DATABASE_NAME,null,DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_NEW_TABLE = "CREATE TABLE"+ TABLE_BOOK +"("
                                + KEY_ID +"INTEGER PRIMARY KEY ,"+KEY_NAME+"TEXT ,"
                                + KEY_CATEGORY + "TEXT," + KEY_AUTHER + "TEXT," + KEY_QUNTITY + "TEXT"+")"; 

    db.execSQL(CREATE_NEW_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("DROP TABLE IF EXITS"+TABLE_BOOK);
    onCreate(db);

}

//add
public void addAllDatabase(Book book){
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues value =new ContentValues();
    value.put(KEY_NAME,book.getName());
    value.put(KEY_CATEGORY,book.getCat());
    value.put(KEY_AUTHER,book.getAuth());
    value.put(KEY_QUNTITY,book.getQunt());

    db.insert(TABLE_BOOK, null, value);

    db.close();     
}

//read single

public Book getSingleBook(int id){

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_BOOK,new String[]{KEY_NAME,KEY_CATEGORY,KEY_AUTHER,KEY_QUNTITY
            },KEY_ID+"=?",new String[]{String.valueOf(KEY_ID)},null,null,null,null);
    if(cursor != null){
        cursor.moveToFirst();
    }

    Book book=new Book(Integer.parseInt(cursor.getString(0)),cursor.getString(1),cursor.getString(2)
            ,cursor.getString(3),cursor.getString(4));

    return book;        
}

logcat:

09-28 17:40:14.055: E/Trace(8437): error opening trace file: No such file or directory (2)
09-28 17:40:29.025: E/SQLiteLog(8437): (1) near "TABLEbook": syntax error
09-28 17:40:29.035: E/AndroidRuntime(8437): FATAL EXCEPTION: main
09-28 17:40:29.035: E/AndroidRuntime(8437): android.database.sqlite.SQLiteException: near "TABLEbook": syntax error (code 1): , while compiling: CREATE TABLEbook(idINTEGER PRIMARY KEY ,nameTEXT ,categoryTEXT,autherTEXT,quntityTEXT)
09-28 17:40:29.035: E/AndroidRuntime(8437):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
09-28 17:40:29.035: E/AndroidRuntime(8437):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
09-28 17:40:29.035: E/AndroidRuntime(8437):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
09-28 17:40:29.035: E/AndroidRuntime(8437):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
09-28 17:40:29.035: E/AndroidRuntime(8437):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
09-28 17:40:29.035: E/AndroidRuntime(8437):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
09-28 17:40:29.035: E/AndroidRuntime(8437):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)
09-28 17:40:29.035: E/AndroidRuntime(8437):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
09-28 17:40:29.035: E/AndroidRuntime(8437):     at com.sam.bookcolector.DatabaseHelper.onCreate(DatabaseHelper.java:31)
09-28 17:40:29.035: E/AndroidRuntime(8437):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
09-28 17:40:29.035: E/AndroidRuntime(8437):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
09-28 17:40:29.035: E/AndroidRuntime(8437):     at com.sam.bookcolector.DatabaseHelper.addAllDatabase(DatabaseHelper.java:44)
09-28 17:40:29.035: E/AndroidRuntime(8437):     at com.sam.bookcolector.MainActivity.onClick(MainActivity.java:47)
09-28 17:40:29.035: E/AndroidRuntime(8437):     at android.view.View.performClick(View.java:4084)
09-28 17:40:29.035: E/AndroidRuntime(8437):     at android.view.View$PerformClick.run(View.java:16966)
09-28 17:40:29.035: E/AndroidRuntime(8437):     at android.os.Handler.handleCallback(Handler.java:615)
09-28 17:40:29.035: E/AndroidRuntime(8437):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-28 17:40:29.035: E/AndroidRuntime(8437):     at android.os.Looper.loop(Looper.java:137)
09-28 17:40:29.035: E/AndroidRuntime(8437):     at android.app.ActivityThread.main(ActivityThread.java:4965)
09-28 17:40:29.035: E/AndroidRuntime(8437):     at java.lang.reflect.Method.invokeNative(Native Method)
09-28 17:40:29.035: E/AndroidRuntime(8437):     at java.lang.reflect.Method.invoke(Method.java:511)
09-28 17:40:29.035: E/AndroidRuntime(8437):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
09-28 17:40:29.035: E/AndroidRuntime(8437):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
09-28 17:40:29.035: E/AndroidRuntime(8437):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:3)

你忘了多个空格。您需要在连接空间上避免使用名称连接SQL。例如。在onCreate:

String CREATE_NEW_TABLE = "CREATE TABLE "+ TABLE_BOOK +" ( "
                            + KEY_ID +" INTEGER PRIMARY KEY, "+KEY_NAME+" TEXT, "
                            + KEY_CATEGORY + " TEXT, " + KEY_AUTHER + " TEXT, " + KEY_QUNTITY + " TEXT)"; 

在onUpgrade中你应该在这里修复它:

db.execSQL("DROP TABLE IF EXISTS "+TABLE_BOOK);

如果我们采用最后一个例子,它会在你的代码中生成这个SQL:

DROP TABLE IF EXITSbook

但它应该是:

DROP TABLE IF EXISTS book