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