Sqlite更新错误按钮保存和删除

时间:2016-10-30 18:43:39

标签: android android-sqlite

我创建了dbNote.sqlite(int id,TEXT INDEX)

public class MainActivity extends AppCompatActivity {
    TextView txtNote;
    Button btnSave,btnDelete;`enter code here
    String DATABASE_NAME="dbNote.sqlite";
    private static final String DB_PATH_SUFFIX = "/databases/";
    SQLiteDatabase database=null;

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

        addControls();
        addEvents();
        ShowPart();
    }
    private void ShowPart() {
        database = openOrCreateDatabase(DATABASE_NAME, MODE_PRIVATE, null);
        Cursor cursor = database.query("NOTE", null,null,null,null,null,null);
        while (cursor.moveToNext())
        {
            String note = cursor.getString(1);
            txtNote.setText(note);
        }

        cursor.close();
    }

    private void addEvents() {
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ContentValues row = new ContentValues();
                String index = txtNote.getText().toString();
                row.put("INDEX",index);
                database.update("NOTE",row,"id=1",null);
                ShowPart();
            }
        });
        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ContentValues row = new ContentValues();
                row.put("INDEX"," ");
                database.update("NOTE",row,"id=1",null);
                //ShowPart();
            }
        });
    }

    private void addControls() {
        txtNote= (TextView) findViewById(R.id.txtNote);
        btnSave= (Button) findViewById(R.id.btnSave);
        btnDelete= (Button) findViewById(R.id.btnDelete);
    }

    private void processCopy() {
        File dbFile = getDatabasePath(DATABASE_NAME);
        if (!dbFile.exists())
        {
            try
            {
                CopyDataBaseFromAsset();
            }
            catch (Exception e)
            {

            }
        }
    }

    private void CopyDataBaseFromAsset() {
        try
        {
            InputStream myInput=getAssets().open(DATABASE_NAME);
            String outFileName = getDatabasePath();
            File f = new File(getApplicationInfo().dataDir + DB_PATH_SUFFIX);
            if(!f.exists())
            {
                f.mkdir();
            }
            OutputStream myOutput = new FileOutputStream(outFileName);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0)
            {
                myOutput.write(buffer, 0, length);
            }
            myOutput.flush();
            myOutput.close();
            myInput.close();
        }
        catch (Exception ex)
        {
            Log.e("Error",ex.toString());
        }
    }
    private String getDatabasePath() {
        return getApplicationInfo().dataDir + DB_PATH_SUFFIX+ DATABASE_NAME;
    }
}
10-30 14:31:47.877 29101-29101/com.it.titun.testnotedatabase E/SQLiteLog: (1) near "INDEX": syntax error
10-30 14:31:47.877 29101-29101/com.it.titun.testnotedatabase D/AndroidRuntime: Shutting down VM
10-30 14:31:47.877 29101-29101/com.it.titun.testnotedatabase E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.it.titun.testnotedatabase, PID: 29101
                                                                           android.database.sqlite.SQLiteException: near "INDEX": syntax error (code 1): , while compiling: UPDATE NOTE SET INDEX=? WHERE id=1
                                                                               at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                               at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                                                                               at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                                                                               at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                               at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                               at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                               at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1574)
                                                                               at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1522)
                                                                               at com.it.titun.testnotedatabase.MainActivity$2.onClick(MainActivity.java:67)
                                                                               at android.view.View.performClick(View.java:4756)
                                                                               at android.view.View$PerformClick.run(View.java:19749)
                                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:135)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:372)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

1 个答案:

答案 0 :(得分:0)

SQLite Keywords表示INDEX是关键字(#60)。因此,您必须使用"`来引用此列名称,如:

row.put("`INDEX`",index);