0

运行下面的代码后,我的应用程序没有响应。代码对我来说似乎是正确的,但无法理解发生了什么问题。

package com.navigationsystem;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.hardware.SensorManager;
import android.util.Log;

public class DBAdapter {

    public static final String NODE_ID="nodeID";
    public static final String NODE_NAME="nodeName";
    public static final String VALUE1="value1";
    public static final String EDGE_ID="edgeID";
    public static final String TOTAL ="total";
    public static final String SOURCE_NODE_ID ="sourceNodeID";
    public static final String DESTINATION_NODE_ID ="destinationNodeID";

    private static final String TAG = "DBAdapter";
    private static final String DATABASE_NAME = "Graph";
    private static final String DATABASE_TABLE1 = "nodes";
    private static final String DATABASE_TABLE2 = "edges";
    private static final int DATABASE_VERSION = 1 ;

    private static final String DATABASE_CREATE ="create table nodes ( " 
        +"nodeID int PRIMARY KEY AUTO_INCREMENT NOT NULL," 
        +"nodeName varchar (20) NOT NULL," 
        +"value1 int NULL," 
        +"edgeID int NULL," 
        +"total int NOT NULL );"

        +"create table edges ( edgeID int PRIMARY KEY AUTO_INCREMENT," 
        +"sourceNodeID int NOT NULL ," 
        +"destinationNodeID int NOT NULL ," 
        +"value1 int NOT NULL );";


    private final Context context;
    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx)
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        DatabaseHelper(Context context)
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db)
        {
            db.execSQL(DATABASE_CREATE);
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion,int newVersion)
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion
                    + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS titles");
            onCreate(db);
        }
    }

    public DBAdapter open() throws SQLException
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }


    public void close()
    {
        DBHelper.close();
    }

    public long addEdges(String nodeName,String value1,String edgeID) throws SQLException
    {

        ContentValues cv = new ContentValues();
        cv.put(NODE_NAME, nodeName);
        cv.put(VALUE1, value1);
        cv.put(EDGE_ID, edgeID);
        return db.insert(DATABASE_TABLE1,null,cv);

    }


}

和数据库调用者类是:

package com.navigationsystem;

import android.app.Activity;
import android.os.Bundle;


public class NavigationSystemActivity extends Activity
{

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        final DBAdapter db = new DBAdapter(this);
        db.open();
        db.addEdges("A","4","1");
        db.close();
    }
}

我想将值存储在数据库中,但既没有创建表,也没有运行我的应用程序。

请纠正我做错了什么。

谢谢。

编辑:logcat 错误

05-25 11:56:49.583: E/AndroidRuntime(9622): FATAL EXCEPTION: main
05-25 11:56:49.583: E/AndroidRuntime(9622): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.navigationsystem/com.navigationsystem.NavigationSystemActivity}: android.database.sqlite.SQLiteException: near "AUTO_INCREMENT": syntax error: , while compiling: create table nodes ( nodeID int PRIMARY KEY AUTO_INCREMENT NOT NULL,nodeName varchar (20) NOT NULL,value1 int NULL,edgeID int NULL,total int NOT NULL );
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.os.Looper.loop(Looper.java:137)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.app.ActivityThread.main(ActivityThread.java:4424)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at java.lang.reflect.Method.invokeNative(Native Method)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at java.lang.reflect.Method.invoke(Method.java:511)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at dalvik.system.NativeStart.main(Native Method)
05-25 11:56:49.583: E/AndroidRuntime(9622): Caused by: android.database.sqlite.SQLiteException: near "AUTO_INCREMENT": syntax error: , while compiling: create table nodes ( nodeID int PRIMARY KEY AUTO_INCREMENT NOT NULL,nodeName varchar (20) NOT NULL,value1 int NULL,edgeID int NULL,total int NOT NULL );
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:134)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:260)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:84)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1899)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1839)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at com.navigationsystem.DBAdapter$DatabaseHelper.onCreate(DBAdapter.java:97)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:165)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at com.navigationsystem.DBAdapter.open(DBAdapter.java:112)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at com.navigationsystem.NavigationSystemActivity.onCreate(NavigationSystemActivity.java:18)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.app.Activity.performCreate(Activity.java:4465)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-25 11:56:49.583: E/AndroidRuntime(9622):     ... 11 more
4

4 回答 4

1

你犯了一个小的语法错误。更改AUTO_INCREMENTAUTOINCREMENT


编辑:

您还提到只有一个表正在创建。这可能是因为您试图将两个创建语句合并为一个。试试这个:

private static final String DATABASE_CREATE_1 = "CREATE TABLE NODES ( ... );";
private static final String DATABASE_CREATE_2 = "CREATE TABLE EDGES ( ... );";

然后在您的onCreate方法中,调用execSQL每个字符串:

db.execSQL(DATABASE_CREATE_1);
db.execSQL(DATABASE_CREATE_2);

在更改生效之前,您需要清除现有数据库。只有在您这样做之后,才会onCreate再次调用它。

设置 --> 应用程序 --> [应用程序名称] --> 清除数据

于 2012-05-25T07:09:26.830 回答
0

需要的关键字是“自动增量”而不是 AUTO_INCREMENT。

于 2012-05-25T08:08:01.897 回答
0

使用AUTOINCREMENT而不是AUTO_INCREMENT 尝试此查询:

create table nodes ( nodeID int PRIMARY KEY AUTOINCREMENT NOT NULL,nodeName varchar (20) NOT NULL,value1 int NULL,edgeID int NULL,total int NOT NULL )
于 2012-05-25T07:11:13.283 回答
0

尝试...

private static final String DATABASE_CREATE ="create table nodes ( " 
    +"nodeID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," 
    +"nodeName VARCHAR (20) NOT NULL," 
    +"value1 INTEGER," 
    +"edgeID INTEGER," 
    +"total INTEGER NOT NULL );"

    +"create table edges ( edgeID INTEGER PRIMARY KEY AUTOINCREMENT," 
    +"sourceNodeID INTEGER NOT NULL ," 
    +"destinationNodeID INTEGER NOT NULL ," 
    +"value1 INTEGER NOT NULL );";

此外,在尝试此操作之前,您需要删除现有数据库(或清除数据),因为 如果数据库已经存在,则不会再次调用onCreate(...)您的方法。SQLiteOpenHelper

于 2012-05-25T07:26:50.930 回答