在添加到数据库之前检查

时间:2013-12-20 13:00:35

标签: android database eclipse sqlite

我有这个代码,应该在通过检查其isbn数字

添加图书之前进行搜索

这是我的Dbhelper类中的isExist方法

public boolean isExist(String isbnNo)
{
    SQLiteDatabase db;
    db = this.getWritableDatabase();
    String query= "SELECT * FROM "+TABLE_NAME +"WHERE "+KEY_ISBN+"='"+isbnNo+"'";      

   Cursor cursor=db.rawQuery(query, null);

    if(cursor.getCount()<1) // UserName Not Exist
    {
        cursor.close();
        return true;
    }
    else 
    {
    cursor.close();
    return false; 
    }               
}   

这是我的添加活动类

中的添加方法
private void saveData(){
    dataBase=mHelper.getWritableDatabase();
    ContentValues values=new ContentValues();
    DbHelper dbHelper = new DbHelper(getApplicationContext());
    values.put(DbHelper.KEY_NAME,name);
    values.put(DbHelper.KEY_AUTHOR,author );
    values.put(DbHelper.KEY_ISBN,isbn );
    values.put(DbHelper.KEY_COPIES,copy );
    values.put(DbHelper.KEY_INFO,info );
    System.out.println("");
    if(isUpdate)
    {    
        //update database with new data 
        dataBase.update(DbHelper.TABLE_NAME, values, DbHelper.KEY_ID+"="+id, null);
    }
    else
    {
        //insert data into database
    if(!dbHelper.isExist(isbn))
            dataBase.insert(DbHelper.TABLE_NAME, null, values);
    else 
        Toast.makeText(getApplicationContext(), "already exist", Toast.LENGTH_LONG).show();
    }
    //close database
    dataBase.close();
    finish();


}

onClick方法

public void onClick(View v) {
    name=edit_name.getText().toString().trim();
    author=edit_author.getText().toString().trim();
    isbn=edit_isbn.getText().toString().trim();
    copy=edit_copy.getText().toString().trim();
    info=edit_info.getText().toString().trim();
    if(name.length()>0 && author.length()>0 && isbn.length()>0 && copy.length()>0 && info.length()>0)
    {
        saveData();
    }
    else
    {
        AlertDialog.Builder alertBuilder=new AlertDialog.Builder(AdminAddBook.this);
        alertBuilder.setTitle("Invalid Data");
        alertBuilder.setMessage("Please, Enter valid data");
        alertBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();

            }
        });
        alertBuilder.create().show();
    }

}

我得到的logcat

          12-20 11:39:25.160: I/Choreographer(900): Skipped 151 frames!  The application may be doing too much work on its main thread.

          12-20 11:39:25.200: D/gralloc_goldfish(900): Emulator without GPU emulation detected.

          12-20 11:39:25.640: I/Choreographer(900): Skipped 39 frames!  The application may be doing too much work on its main thread.

          12-20 11:39:36.770: D/dalvikvm(900): GC_FOR_ALLOC freed 126K, 7% free 3083K/3284K, paused 83ms, total 100ms

          12-20 11:39:48.430: I/Choreographer(987): Skipped 44 frames!  The application may be doing too much work on its main thread.

          12-20 11:39:48.500: D/gralloc_goldfish(987): Emulator without GPU emulation detected.

          12-20 08:40:02.735: D/dalvikvm(987): GC_FOR_ALLOC freed 123K, 7% free 3079K/3280K, paused 111ms, total 117ms

          12-20 08:40:02.775: I/Choreographer(987): Skipped 43 frames!  The application may be doing too much work on its main thread.

          12-20 08:40:05.815: I/Choreographer(987): Skipped 49 frames!  The application may be doing too much work on its main thread.

          12-20 08:40:13.965: D/dalvikvm(987): GC_FOR_ALLOC freed 112K, 6% free 3243K/3432K, paused 87ms, total 130ms

          12-20 08:40:14.305: I/Choreographer(987): Skipped 238 frames!  The application may be doing too much work on its main thread.

          12-20 08:40:14.565: I/Choreographer(987): Skipped 46 frames!  The application may be doing too much work on its main thread.

          12-20 08:40:25.535: I/Choreographer(987): Skipped 112 frames!  The application may be doing too much work on its main thread.

          12-20 08:40:32.085: D/dalvikvm(987): GC_FOR_ALLOC freed 97K, 5% free 3660K/3832K, paused 33ms, total 39ms

          12-20 08:40:42.495: E/SQLiteLog(987): (1) near "=": syntax error

          12-20 08:40:42.495: D/AndroidRuntime(987): Shutting down VM

          12-20 08:40:42.505: W/dalvikvm(987): threadid=1: thread exiting with uncaught exception (group=0xb4a57b90)

          12-20 08:40:42.525: E/AndroidRuntime(987): FATAL EXCEPTION: main

          12-20 08:40:42.525: E/AndroidRuntime(987): Process: com.example.mobilib, PID: 987

          12-20 08:40:42.525: E/AndroidRuntime(987): android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , while compiling: SELECT * FROM BookWHERE isbn='123'

          12-20 08:40:42.525: E/AndroidRuntime(987):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)

          12-20 08:40:42.525: E/AndroidRuntime(987):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)

          12-20 08:40:42.525: E/AndroidRuntime(987):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)

          12-20 08:40:42.525: E/AndroidRuntime(987):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)

          12-20 08:40:42.525: E/AndroidRuntime(987):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)

          12-20 08:40:42.525: E/AndroidRuntime(987):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)

          12-20 08:40:42.525: E/AndroidRuntime(987):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)

          12-20 08:40:42.525: E/AndroidRuntime(987):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)

          12-20 08:40:42.525: E/AndroidRuntime(987):    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)

          12-20 08:40:42.525: E/AndroidRuntime(987):    at com.example.mobilib.DbHelper.isExist(DbHelper.java:37)

          12-20 08:40:42.525: E/AndroidRuntime(987):    at com.example.mobilib.AdminAddBook.saveData(AdminAddBook.java:146)

          12-20 08:40:42.525: E/AndroidRuntime(987):    at com.example.mobilib.AdminAddBook.onClick(AdminAddBook.java:106)

          12-20 08:40:42.525: E/AndroidRuntime(987):    at android.view.View.performClick(View.java:4424)

          12-20 08:40:42.525: E/AndroidRuntime(987):    at android.view.View$PerformClick.run(View.java:18383)

          12-20 08:40:42.525: E/AndroidRuntime(987):    at android.os.Handler.handleCallback(Handler.java:733)

          12-20 08:40:42.525: E/AndroidRuntime(987):    at android.os.Handler.dispatchMessage(Handler.java:95)

          12-20 08:40:42.525: E/AndroidRuntime(987):    at android.os.Looper.loop(Looper.java:137)

          12-20 08:40:42.525: E/AndroidRuntime(987):    at android.app.ActivityThread.main(ActivityThread.java:4998)

          12-20 08:40:42.525: E/AndroidRuntime(987):    at java.lang.reflect.Method.invokeNative(Native Method)

          12-20 08:40:42.525: E/AndroidRuntime(987):    at java.lang.reflect.Method.invoke(Method.java:515)

          12-20 08:40:42.525: E/AndroidRuntime(987):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)

          12-20 08:40:42.525: E/AndroidRuntime(987):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)

          12-20 08:40:42.525: E/AndroidRuntime(987):    at dalvik.system.NativeStart.main(Native Method)

我遇到的问题是,当我点击我的保存按钮时,应用程序将崩溃

有人可以帮我查一下吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

你需要在书和sql字符串中的位置之间留一个空白。

答案 1 :(得分:0)

尝试这样:

您的查询有错误,如表名和克劳斯之间没有空格。

public boolean isExist(String isbnNo)
{
    SQLiteDatabase db;
    db = this.getWritableDatabase();
    String query= "SELECT * FROM "+TABLE_NAME +" WHERE "+KEY_ISBN+"='"+isbnNo+"'";      

   Cursor cursor=db.rawQuery(query, null);

    if(cursor.getCount()<1) // UserName Not Exist
    {
        cursor.close();
        return true;
    }
    else 
    {
    cursor.close();
    return false; 
    }               
}