引起:android.database.sqlite.SQLiteException:near“”:语法错误(代码1):

时间:2013-11-27 18:53:49

标签: java android sql database sqlite

嗨,我一直收到这个错误,我已经多次查看我的数据库结构,看不出我做错了什么

以下是我的错误

11-27 18:42:17.590: E/AndroidRuntime(16335): FATAL EXCEPTION: main
11-27 18:42:17.590: E/AndroidRuntime(16335): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.doja.myapp/com.doja.myapp.Database}: android.database.sqlite.SQLiteException: near "Database": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS Device Database (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, date TEXT NOT NULL, time TEXT NOT NULL, duration TEXT NOT NULL);
11-27 18:42:17.590: E/AndroidRuntime(16335):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
11-27 18:42:17.590: E/AndroidRuntime(16335):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
11-27 18:42:17.590: E/AndroidRuntime(16335):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
11-27 18:42:17.590: E/AndroidRuntime(16335):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
11-27 18:42:17.590: E/AndroidRuntime(16335):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-27 18:42:17.590: E/AndroidRuntime(16335):    at android.os.Looper.loop(Looper.java:137)
11-27 18:42:17.590: E/AndroidRuntime(16335):    at android.app.ActivityThread.main(ActivityThread.java:4898)
11-27 18:42:17.590: E/AndroidRuntime(16335):    at java.lang.reflect.Method.invokeNative(Native Method)
11-27 18:42:17.590: E/AndroidRuntime(16335):    at java.lang.reflect.Method.invoke(Method.java:511)
11-27 18:42:17.590: E/AndroidRuntime(16335):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
11-27 18:42:17.590: E/AndroidRuntime(16335):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
11-27 18:42:17.590: E/AndroidRuntime(16335):    at dalvik.system.NativeStart.main(Native Method)
11-27 18:42:17.590: E/AndroidRuntime(16335): Caused by: android.database.sqlite.SQLiteException: near "Database": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS Device Database (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, date TEXT NOT NULL, time TEXT NOT NULL, duration TEXT NOT NULL);
11-27 18:42:17.590: E/AndroidRuntime(16335):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
11-27 18:42:17.590: E/AndroidRuntime(16335):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1012)
11-27 18:42:17.590: E/AndroidRuntime(16335):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:623)
11-27 18:42:17.590: E/AndroidRuntime(16335):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
11-27 18:42:17.590: E/AndroidRuntime(16335):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
11-27 18:42:17.590: E/AndroidRuntime(16335):    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
11-27 18:42:17.590: E/AndroidRuntime(16335):    at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)
11-27 18:42:17.590: E/AndroidRuntime(16335):    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
11-27 18:42:17.590: E/AndroidRuntime(16335):    at com.doja.myapp.DBAdapter$DBHelper.onCreate(DBAdapter.java:60)
11-27 18:42:17.590: E/AndroidRuntime(16335):    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
11-27 18:42:17.590: E/AndroidRuntime(16335):    at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
11-27 18:42:17.590: E/AndroidRuntime(16335):    at com.doja.myapp.DBAdapter.open(DBAdapter.java:73)
11-27 18:42:17.590: E/AndroidRuntime(16335):    at com.doja.myapp.Database.openDB(Database.java:74)
11-27 18:42:17.590: E/AndroidRuntime(16335):    at com.doja.myapp.Database.onCreate(Database.java:23)
11-27 18:42:17.590: E/AndroidRuntime(16335):    at android.app.Activity.performCreate(Activity.java:5206)
11-27 18:42:17.590: E/AndroidRuntime(16335):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
11-27 18:42:17.590: E/AndroidRuntime(16335):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
11-27 18:42:17.590: E/AndroidRuntime(16335):    ... 11 more
11-27 18:42:17.630: D/dalvikvm(16335): GC_CONCURRENT freed 170K, 10% free 12372K/13639K, paused 12ms+2ms, total 30ms

这是数据库类

package com.doja.myapp;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.widget.TextView;

public class Database extends Activity {

DBAdapter myDb;

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

    Intent intent = getIntent();

    openDB();

    //long newId = myDb.insertRow("Doja", "12/12/2013", "06:34", "2.34 seconds");

    // Query for the record we just added.
    // Use the ID:

    //Cursor cursor = myDb.getAllRows();
    //Cursor cursor = myDb.getRow(newId);
    //displayRecordSet(cursor);

}

private void displayRecordSet(Cursor cursor) {
    // TODO Auto-generated method stub
    String message = "";
    // populate the message from the cursor

    // Reset cursor to start, checking to see if there's data:
    if (cursor.moveToFirst()) {
        do {
            // Process the data:
            int id = cursor.getInt(DBAdapter.COL_ROWID);
            String name = cursor.getString(DBAdapter.COL_NAME);
            String date = cursor.getString(DBAdapter.COL_DATE);
            String time = cursor.getString(DBAdapter.COL_TIME);
            String duration = cursor.getString(DBAdapter.COL_DURATION);

            // Append data to the message:
            message += "id:" + id
                       +", Name:" + name
                       +", Date:" + date
                       +", Time:" + time
                       +", Duration:" + duration
                       +"\n";
        } while(cursor.moveToNext());
    }

    cursor.close();
    displayText(message);
}

private void displayText(String message) {
    // TODO Auto-generated method stub
    TextView textView = (TextView) findViewById(R.id.textView);
    textView.setText(message);
}

private void openDB() {
    // TODO Auto-generated method stub
    myDb = new DBAdapter(this);
    myDb.open();
    Cursor cursor = myDb.getAllRows();
}

@Override
protected void onDestroy() {
    super.onDestroy();  
    closeDB();
}

private void closeDB() {
    // TODO Auto-generated method stub
    myDb.close();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.database, menu);
    return true;
}

}

我的DBAdapater课程

package com.doja.myapp;

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

public class DBAdapter {

private static final String TAG = "DBAdapter";

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "Results";
static final String DATABASE_TABLE = "Device Database";

private static final String KEY_ROWID = "_id";
public static final int COL_ROWID = 0;

static final String KEY_NAME = "name";
static final String KEY_DATE = "date";
static final String KEY_TIME = "time";
static final String KEY_DURATION = "duration";

public static final int COL_NAME = 1;
public static final int COL_DATE = 2;
public static final int COL_TIME = 3;
public static final int COL_DURATION = 4;

public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_NAME, KEY_DATE, KEY_TIME, KEY_DURATION};

private static final String DATABASE_TABLE_CREATE =
            "CREATE TABLE IF NOT EXISTS " + DATABASE_TABLE + " (" 
            + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + KEY_NAME + " TEXT NOT NULL, " 
            + KEY_DATE + " TEXT NOT NULL, " 
            + KEY_TIME + " TEXT NOT NULL, " 
            + KEY_DURATION + " TEXT NOT NULL"
            + ");";

private DBHelper myDBHelper;
private final Context myContext;
private SQLiteDatabase myDatabase;

public DBAdapter(Context ctx) {
    this.myContext = ctx;
    myDBHelper = new DBHelper(myContext);
}

private static class DBHelper extends SQLiteOpenHelper {    

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


    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(DATABASE_TABLE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }
}

// Open the database connection.
public DBAdapter open() {
    myDatabase = myDBHelper.getReadableDatabase();
    return this;
}

// Close the database connection.
public void close() {
    myDBHelper.close();
}

// Add a new set of values to the database.
public long insertRow(String name, String date, String time, String duration) {

    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_NAME, name);
    initialValues.put(KEY_DATE, date);
    initialValues.put(KEY_TIME, time);
    initialValues.put(KEY_DURATION, duration);

    // Insert it into the database.
    return myDatabase.insert(DATABASE_TABLE, null, initialValues);
}

// Delete a row from the database, by rowId (primary key)
public boolean deleteRow(long rowId) {
    String where = KEY_ROWID + "=" + rowId;
    return myDatabase.delete(DATABASE_TABLE, where, null) != 0;
}

public void deleteAll() {
    Cursor c = getAllRows();
    long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
    if (c.moveToFirst()) {
        do {
            deleteRow(c.getLong((int) rowId));              
        } while (c.moveToNext());
    }
    c.close();
}

// Return all data in the database.
public Cursor getAllRows() {
    String where = null;
    Cursor c =  myDatabase.query(true, DATABASE_TABLE, ALL_KEYS, 
                        where, null, null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}

// Get a specific row (by rowId)
public Cursor getRow(long rowId) {
    String where = KEY_ROWID + "=" + rowId;
    Cursor c =  myDatabase.query(true, DATABASE_TABLE, ALL_KEYS, 
                    where, null, null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}

// Change an existing row to be equal to new data.
public boolean updateRow(long rowId, String name, String date, String time, String duration) {
    String where = KEY_ROWID + "=" + rowId;

    ContentValues newValues = new ContentValues();
    newValues.put(KEY_NAME, name);
    newValues.put(KEY_DATE, date);
    newValues.put(KEY_TIME, time);
    newValues.put(KEY_DURATION, duration);

    // Insert it into the database.
    return myDatabase.update(DATABASE_TABLE, newValues, where, null) != 0;
}

}

并在我的主要活动中

public void openDatabase(View view) {
        Intent openDatabase = new Intent(MainActivity.this, Database.class);
        MainActivity.this.startActivity(openDatabase);
        startActivity(openDatabase);    
    }

1 个答案:

答案 0 :(得分:3)

你不能打电话给你的桌子

Device Database 

你的姓名不能留空。建议,这将有效:

DeviceDatabase
Device_Database