mardi 27 octobre 2015

Android Syntax Error with Database: Can't Find It

I am doing a simple database in android, 1 column where you add names of animals. When I try to run it the app won't even open and I get a syntax error message. I've looked over it for a couple hours and just cannot find what it is talking about. Hoping someone can find what it is talking about.

Error:

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

Database Code:

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;
    }


}

Aucun commentaire:

Enregistrer un commentaire