Android SQL数据库问题

时间:2015-01-27 02:02:37

标签: android sqlite

首先,我想告诉大家我是SQL的新手(昨天我开始这么做了)。我在编写或从我的数据库中检索信息时遇到了问题,说实话,我有点害怕触摸某些东西并弄乱它。这是我的 的 DatabaseAdapter.java

public class DatabaseAdapter {
DatabaseHelper helper;

public DatabaseAdapter(Context context){
    helper = new DatabaseHelper(context);
}
public long insertData(String cName, String cText){
    SQLiteDatabase  db= helper.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(DatabaseHelper.NAME, className);
    contentValues.put(DatabaseHelper.NOTES,classNotes );
    long id=db.insert(DatabaseHelper.TABLE_NAME,null,contentValues);
    return id;

}
public String getAllData(){
    >>>SQLiteDatabase db= helper.getWritableDatabase();
    String[] columns = {DatabaseHelper.UID, DatabaseHelper.NAME, DatabaseHelper.TEXT};
    Cursor cursor=db.query(DatabaseHelper.TABLE_NAME, columns, null, null, null, null, null);
    StringBuffer buffer = new StringBuffer();
    while(cursor.moveToNext()){
        int cid=cursor.getInt(0);
        String  name = cursor.getString(1);
        String text = cursor.getString(2);
        buffer.append(cid + "" + name + "" + text + "/n");


    }
    return buffer.toString();
}

class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "DataBase";
    public static final String TABLE_NAME = "DATABASE_TABLE";
    public static final String UID = "_id";
    public static final String NAME = "Name";
    public static final int DATABASE_VERSION = 4;
    public static final String TEXT = "Text";

    public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME + " VARCHAR(30),"+TEXT+" VARCHAR(999999));";
    public static final String DROP_TABLE = "DROP TABLE IF EXISTS" +TABLE_NAME;

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE);
        Log.i("Table Created", "Table Created");


    }

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

        db.execSQL(DROP_TABLE);
        >>>onCreate(db);
        Log.i("Table Upgraded", "Table Upgraded");

    }

}

} 我用>>>标记了logCat出现问题的区域。 我也在尝试接收数据的类中遇到问题。 的 Load_Text.java

public class Load_Text extends ActionBarActivity {
TextView dataText;
String data;
DatabaseAdapter databaseHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.load_text);
    databaseHelper = new DatabaseAdapter(this);
    dataText=(TextView)findViewById(R.id.textView3);
    >>>data = databaseHelper.getAllData();

    if(databaseHelper.getAllData() == null){
        Toast.makeText(this, "No data found", Toast.LENGTH_LONG).show();
    }else{
        dataText.setText(data);
    }

}

} 这是logCat:

01-26 20:46:35.006  32429-32429/autonote.six.padc.autonote E/SQLiteLog﹕ (1)     near "EXISTSDATABASE_TABLE": syntax error
01-26 20:46:35.006  32429-32429/autonote.six.padc.autonote     D/AndroidRuntime﹕ Shutting down VM
01-26 20:46:35.016  32429-32429/autonote.six.padc.autonote W/dalvikvm﹕     threadid=1: thread exiting with uncaught exception (group=0x417b9898)
01-26 20:46:35.016  32429-32429/autonote.six.padc.autonote     E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{autonote.six.padc.autonote/autonote.six.padc.autonote.Load_Notes}: android.database.sqlite.SQLiteException: near "EXISTSDATABASE_TABLE": syntax error (code 1): , while compiling: DROP TABLE IF EXISTSDATABASE_TABLE
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
            at         android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
           at android.app.ActivityThread.access$700(ActivityThread.java:165)
           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5455)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.database.sqlite.SQLiteException: near "EXISTSDATABASE_TABLE": syntax error (code 1): , while compiling: DROP TABLE IF EXISTSDATABASE_TABLE
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1118)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:691)
        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.executeSql(SQLiteDatabase.java:1794)
        at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1725)
        at autonote.six.padc.autonote.DatabaseAdapter$DatabaseHelper.onUpgrade(DatabaseAdapter.java:72)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:257)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
        at autonote.six.padc.autonote.DatabaseAdapter.getAllData(DatabaseAdapter.java:31)
        at autonote.six.padc.autonote.Load_Notes.onCreate(Load_Text.java:23)
        at android.app.Activity.performCreate(Activity.java:5372)

Load_Text.java 中的另一个错误只是nullPointerException,我可以放心地假设这是由于我的数据库中的语法错误引起的。有人知道如何解决我所犯的错误吗? 谢谢!

1 个答案:

答案 0 :(得分:1)

你错过了一个空格:

Caused by: android.database.sqlite.SQLiteException: near "EXISTSDATABASE_TABLE": syntax error (code 1): , while compiling: DROP TABLE IF EXISTSDATABASE_TABLE

您需要在EXISTS和表名之间留一个空格:

public static final String DROP_TABLE = " DROP TABLE IF EXISTS " + TABLE_NAME;

SQLite(以及大多数/所有SQL实现?)忽略关键字之间的额外空格,因此最好始终在字符串的开头和结尾添加空格。