Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 数据库导致应用程序崩溃_Android_Sqlite - Fatal编程技术网

Android 数据库导致应用程序崩溃

Android 数据库导致应用程序崩溃,android,sqlite,Android,Sqlite,我正在尝试制作一个带有数据库的简单应用程序。 问题是,由于某种原因,该应用程序在启动时崩溃。 不幸的是,崩溃日志并没有告诉我太多,除了创建数据库似乎有问题,但也许这里有人能理解它。 提前谢谢 主要活动 package se.welovecode.wdmmg; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.text.format.Time; import andr

我正在尝试制作一个带有数据库的简单应用程序。 问题是,由于某种原因,该应用程序在启动时崩溃。 不幸的是,崩溃日志并没有告诉我太多,除了创建数据库似乎有问题,但也许这里有人能理解它。 提前谢谢

主要活动

package se.welovecode.wdmmg;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.format.Time;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;


public class MainActivity extends ActionBarActivity {

DBAdapter myDB;
EditText etTransaction;

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

    openDB();
}

protected void onDestroy(){
    super.onDestroy();
    closeDb();
}

private void openDB(){
    myDB = new DBAdapter(this);
    myDB.open();
}
private void closeDb(){

    myDB.close();
}

public void onClick_AddRecord(View v){
    myDB.insertRow("transaction","item","sum");
}
}
DBAdapter

package se.welovecode.wdmmg;

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

public class DBAdapter {

private static final String TAG = "DBAdapter"; //used for logging database version changes

// Field Names:
public static final String KEY_ROWID = "_id";
public static final String KEY_TRANSACTION = "transaction";
public static final String KEY_ITEM = "item";
public static final String KEY_SUM = "sum";

public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_TRANSACTION, KEY_ITEM, KEY_SUM,};

// Column Numbers for each Field Name:
public static final int COL_ROWID = 0;
public static final int COL_TRANSACTION = 1;
public static final int COL_ITEM = 2;
public static final int COL_SUM = 3;

// DataBase info:
public static final String DATABASE_NAME = "db";
public static final String DATABASE_TABLE = "mainTransactions";
public static final int DATABASE_VERSION = 1; // The version number must be incremented each time a change to DB structure occurs.

//SQL statement to create database
private static final String DATABASE_CREATE_SQL = 
        "CREATE TABLE " + DATABASE_TABLE 
        + " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + KEY_TRANSACTION + " TEXT NOT NULL, "
        + KEY_ITEM + " TEXT NOT NULL, "
        + KEY_SUM + " TEXT,"
        + ");";

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


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

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

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

// Add a new set of values to be inserted into the database.
public long insertRow(String transaction, String item, String sum) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_TRANSACTION, transaction);
    initialValues.put(KEY_ITEM, item);
    initialValues.put(KEY_SUM, sum);

    // Insert the data into the database.
    return db.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 db.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 =  db.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 =  db.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 transaction, String item, String sum) {
    String where = KEY_ROWID + "=" + rowId;
    ContentValues newValues = new ContentValues();
    newValues.put(KEY_TRANSACTION, transaction);
    newValues.put(KEY_ITEM, item);
    newValues.put(KEY_SUM, sum);
    // Insert it into the database.
    return db.update(DATABASE_TABLE, newValues, where, null) != 0;
}


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

    @Override
    public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading application's database from version " + oldVersion
                + " to " + newVersion + ", which will destroy all old data!");

        // Destroy old database:
        _db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);

        // Recreate new database:
        onCreate(_db);
    }
}
}
事故日志

   java.lang.RuntimeException: Unable to start activity ComponentInfo{se.welovecode.wdmmg/se.welovecode.wdmmg.MainActivity}: android.database.sqlite.SQLiteException: near "transaction": syntax error (code 1): , while compiling: CREATE TABLE mainTransactions (_id INTEGER PRIMARY KEY AUTOINCREMENT, transaction TEXT NOT NULL, item TEXT NOT NULL, sum TEXT,);
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2814)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2879)
        at android.app.ActivityThread.access$900(ActivityThread.java:182)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1475)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:6141)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
 Caused by: android.database.sqlite.SQLiteException: near "transaction": syntax error (code 1): , while compiling: CREATE TABLE mainTransactions (_id INTEGER PRIMARY KEY AUTOINCREMENT, transaction TEXT NOT NULL, item TEXT NOT NULL, sum TEXT,);
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1093)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:670)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1798)
        at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1729)
        at se.welovecode.wdmmg.DBAdapter$DatabaseHelper.onCreate(DBAdapter.java:132)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
        at     android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
        at se.welovecode.wdmmg.DBAdapter.open(DBAdapter.java:54)
        at se.welovecode.wdmmg.MainActivity.openDB(MainActivity.java:32)
        at se.welovecode.wdmmg.MainActivity.onCreate(MainActivity.java:22)
at android.app.Activity.performCreate(Activity.java:6374)
at         android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2767)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2879)
            at android.app.ActivityThread.access$900(ActivityThread.java:182)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1475)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:6141)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
java.lang.RuntimeException:无法启动活动组件信息{se.welovecode.wdmmg/se.welovecode.wdmmg.MainActivity}:android.database.sqlite.SQLiteException:靠近“事务”:编译时语法错误(代码1):,创建表mainTransactions(_idinteger主键自动递增,事务文本不为NULL,项目文本不为NULL,总和文本,);
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2814)上
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2879)上
在android.app.ActivityThread.access$900(ActivityThread.java:182)
在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1475)上
位于android.os.Handler.dispatchMessage(Handler.java:102)
位于android.os.Looper.loop(Looper.java:145)
位于android.app.ActivityThread.main(ActivityThread.java:6141)
位于java.lang.reflect.Method.invoke(本机方法)
位于java.lang.reflect.Method.invoke(Method.java:372)
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)上
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
原因:android.database.sqlite.SQLiteException:靠近“事务”:语法错误(代码1),编译时:创建表mainTransactions(_idinteger主键自动递增,事务文本不为NULL,项文本不为NULL,总和文本,);
位于android.database.sqlite.SQLiteConnection.nativePrepareStatement(本机方法)
位于android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1093)
位于android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:670)
位于android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
位于android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:59)
位于android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:31)
位于android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1798)
位于android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1729)
位于se.welovecode.wdmmg.DBAdapter$DatabaseHelper.onCreate(DBAdapter.java:132)
位于android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
位于android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
在se.welovecode.wdmmg.DBAdapter.open(DBAdapter.java:54)
位于se.welovecode.wdmmg.MainActivity.openDB(MainActivity.java:32)
位于se.welovecode.wdmmg.MainActivity.onCreate(MainActivity.java:22)
位于android.app.Activity.performCreate(Activity.java:6374)
位于android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2767)上
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2879)上
在android.app.ActivityThread.access$900(ActivityThread.java:182)
在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1475)上
位于android.os.Handler.dispatchMessage(Handler.java:102)
位于android.os.Looper.loop(Looper.java:145)
位于android.app.ActivityThread.main(ActivityThread.java:6141)
位于java.lang.reflect.Method.invoke(本机方法)
位于java.lang.reflect.Method.invoke(Method.java:372)
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)上
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
您在这里出错了

 + KEY_SUM + " TEXT," // remove ,(comma) from last
正确:

String DATABASE_CREATE_SQL = 
    "CREATE TABLE " + DATABASE_TABLE 
    + " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
    + KEY_TRANSACTION + " TEXT NOT NULL, "
    + KEY_ITEM + " TEXT NOT NULL, "
    + KEY_SUM + " TEXT"
    + ");";
还要更改事务列名,因为它是保留的,不能用于查询

public static final String KEY_TRANSACTION = "transaction";

Wordtransaction是SQL中的保留字,不能用作列名。将其更改为transaction_text或类似的内容。

我看到了
sum text,
。看起来逗号是冗余的,只是为了最小化字符串的浓缩:
+KEY_sum+“text”)(无论如何不使用;)。如果我们都开始使用
StringBuilder
来处理这类事情,那就更好了,但是!嗯,是的,也许将来……;)