数据库的Android语法错误:无法找到它

时间:2015-10-27 23:54:55

标签: android sqlite

我在android中做了一个简单的数据库,在1列中你添加了动物的名字。当我尝试运行它时,应用程序甚至无法打开,我收到语法错误消息。我已经看了好几个小时,却找不到它在说什么。希望有人能找到它所说的内容。

错误:

10-27 18:37:07.394 2537-2537/? I/art: Not late-enabling -Xcheck:jni (already on)
10-27 18:37:07.394 2537-2537/? I/art: Late-enabling JIT
10-27 18:37:07.399 2537-2537/? I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
10-27 18:37:07.463 2537-2537/? W/System: ClassLoader referenced unknown path: /data/app/com.example.test.assignment3-2/lib/x86
10-27 18:37:07.691 2537-2537/com.example.test.assignment3 E/SQLiteLog: (1) near "animalName": syntax error
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 D/AndroidRuntime: Shutting down VM
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime: FATAL EXCEPTION: main
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime: Process: com.example.test.assignment3, PID: 2537
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test.assignment3/com.example.test.assignment3.MainActivity}: android.database.sqlite.SQLiteException: near "animalName": syntax error (code 1): , while compiling: CREATE TABLE animals(_id INTEGER PRIMARY KEY AUTOINCREMENT animalName TEXT );
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:  Caused by: android.database.sqlite.SQLiteException: near "animalName": syntax error (code 1): , while compiling: CREATE TABLE animals(_id INTEGER PRIMARY KEY AUTOINCREMENT animalName TEXT );
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at com.example.zwallm00.zackwallmarkassignment3.DBHandler.onCreate(DBHandler.java:27)
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at com.example.test.assignment3.DBHandler.printDatabase(DBHandler.java:53)
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at com.example.test.assignment3.MainActivity.printDatabase(MainActivity.java:39)
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at com.example.test.assignment3.MainActivity.onCreate(MainActivity.java:21)
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:6237)
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148) 
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417) 
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
10-27 18:37:07.692 2537-2537/com.example.test.assignment3 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
10-27 18:37:09.767 2537-2537/? I/Process: Sending signal. PID: 2537 SIG: 9

数据库代码:

package com.example.test.assignment3;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.Cursor;
import android.content.Context;
import android.content.ContentValues;

public class DBHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "animals.db";
    public static final String TABLE_ANIMALS = "animals";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_ANIMALS = "animalName";

    public DBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE " + TABLE_ANIMALS + "(" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT " +
                COLUMN_ANIMALS + " TEXT " +
                ");";
        db.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_ANIMALS);
        onCreate(db);
    }

    //Add Animal
    public void addAnimal(Animals animal){
        ContentValues values = new ContentValues();
        values.put(COLUMN_ANIMALS, animal.get_animalName());
        SQLiteDatabase db = getWritableDatabase();
        db.insert(TABLE_ANIMALS, null, values);
        db.close();
    }
    //Delete Animal
    public  void deleteAnimal(String animalName){
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("DELETE FROM " + TABLE_ANIMALS + " WHERE " + COLUMN_ANIMALS + "=\"" + animalName + "\";");
    }

    //Print Animals
    public String printDatabase(){
        String string = "";
        SQLiteDatabase db = getWritableDatabase();
        String query = "SELECT * FROM " + TABLE_ANIMALS + " WHERE 1";
        Cursor cursor = db.rawQuery(query, null);
        cursor.moveToFirst();
        while(!cursor.isAfterLast()){
            if(cursor.getString(cursor.getColumnIndex("animalName"))!=null){
                string += cursor.getString(cursor.getColumnIndex("animalName"));
                string += "\n";
            }
        }
        db.close();
        return string;
    }


}

1 个答案:

答案 0 :(得分:0)

您在 AUTOINCREMENT 之后缺少逗号,,修复此问题并且它应该可以正常工作。

将您的查询更改为以下内容,

String query = "CREATE TABLE " + TABLE_ANIMALS + "(" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_ANIMALS + " TEXT " +
                ");";

可能要求卸载并全新安装该应用以进行此更改。