0

我的简单账簿代码存在一些问题。

单击保存按钮时,我想插入一行。

我检查了我的表创建成功。

save.setOnClickListener(new Button.OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            mDbOpenHelper.open();

            try{
            mDbOpenHelper.insertColumn(
                    rButton, 
                    mEditTexts[1].getText().toString().trim(), 
                    spinnerSelected, 
                    mEditTexts[0].getText().toString().trim());
            }catch(Exception e){
                Log.d("TAG","error");
            }

            mDbOpenHelper.close();
            mCursor.close();

        }

    });

插入列()

// Insert DB
        public long insertColumn(String cashORcard, String amount, String category, String detail){
            mDB = mDBHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put(Database.CreateDB.CASH, cashORcard);
            values.put(Database.CreateDB.AMOUNT, amount);
            values.put(Database.CreateDB.CATEGORY, category);
            values.put(Database.CreateDB.DETAIL, detail);

            return mDB.insert(Database.CreateDB.TABLENAME, null, values);

        }

定义我的数据库表

public class Database {

public static final class CreateDB implements BaseColumns{
    public static String firstCash="0";
    public static String firstAccount="0";
    public static String allMoney="0";
    public static final String CASH="cash";
    public static final String CATEGORY="category";
    public static final String DETAIL="detail";
    public static final String AMOUNT="0";
    public static final String ID="0";
    public static final String TABLENAME="Accountbook";
    public static final String CREATE=
            "CREATE TABLE "+TABLENAME
            +" ( _id integer primary key autoincrement, "
            +" CASH text not null , "
            +" AMOUNT text not null , "
            +" CATEGORY text not null , "
            +" DETAIL text , "
            +" firstCash text not null , "
            +" firstAccount text not null , "
            +" allMoney text not null );";  
    }
}

日志

04-17 02:53:11.471: E/SQLiteLog(877): (1) near "0": syntax error
04-17 02:53:11.501: E/SQLiteDatabase(877): Error inserting cash=cash detail=asdf category=salary 0=567
04-17 02:53:11.501: E/SQLiteDatabase(877): android.database.sqlite.SQLiteException: near "0": syntax error (code 1): , while compiling: INSERT INTO Accountbook(cash,detail,category,0) VALUES (?,?,?,?)
04-17 02:53:11.501: E/SQLiteDatabase(877):  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
04-17 02:53:11.501: E/SQLiteDatabase(877):  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
04-17 02:53:11.501: E/SQLiteDatabase(877):  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
04-17 02:53:11.501: E/SQLiteDatabase(877):  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
04-17 02:53:11.501: E/SQLiteDatabase(877):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
04-17 02:53:11.501: E/SQLiteDatabase(877):  at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
04-17 02:53:11.501: E/SQLiteDatabase(877):  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
04-17 02:53:11.501: E/SQLiteDatabase(877):  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
04-17 02:53:11.501: E/SQLiteDatabase(877):  at com.android.accountbook_edit.DbOpenHelper.insertColumn(DbOpenHelper.java:82)
04-17 02:53:11.501: E/SQLiteDatabase(877):  at com.android.accountbook_edit.Income$3.onClick(Income.java:99)
04-17 02:53:11.501: E/SQLiteDatabase(877):  at android.view.View.performClick(View.java:4204)
04-17 02:53:11.501: E/SQLiteDatabase(877):  at android.view.View$PerformClick.run(View.java:17355)
04-17 02:53:11.501: E/SQLiteDatabase(877):  at android.os.Handler.handleCallback(Handler.java:725)
04-17 02:53:11.501: E/SQLiteDatabase(877):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-17 02:53:11.501: E/SQLiteDatabase(877):  at android.os.Looper.loop(Looper.java:137)
04-17 02:53:11.501: E/SQLiteDatabase(877):  at android.app.ActivityThread.main(ActivityThread.java:5041)
04-17 02:53:11.501: E/SQLiteDatabase(877):  at java.lang.reflect.Method.invokeNative(Native Method)
04-17 02:53:11.501: E/SQLiteDatabase(877):  at java.lang.reflect.Method.invoke(Method.java:511)
04-17 02:53:11.501: E/SQLiteDatabase(877):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-17 02:53:11.501: E/SQLiteDatabase(877):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-17 02:53:11.501: E/SQLiteDatabase(877):  at dalvik.system.NativeStart.main(Native Method)

需要更多代码,请告诉我!谢谢 !

4

5 回答 5

1

首先,您插入的是rows而不是columns

然后,你日志中的错误就很清楚了

android.database.sqlite.SQLiteException: near "0": ... INSERT INTO Accountbook(cash,detail,category,0) VALUES (?,?,?,?) ...

您正在尝试将值插入到无效的列名中:0

于 2013-04-17T04:58:52.800 回答
0

您已将值传递mEditTexts[0].getText().toString().trim()给字符串详细信息。检查字符串详细信息和ID Database.CreateDB.DETAIL. 如果它显示“0”,那么它应该是“细节”。

于 2013-04-17T05:15:37.250 回答
0

尝试更改您的插入查询,如下所示:

您还需要传递其他剩余列的firstCash,firstAccount,allMoney,因为在 Create Table 查询中您已将其属性定义为 NOT NULL。

public long insertColumn(String cashORcard, String amount, String category, String detail,String cashValue,String AccountVal,String money){
        mDB = mDBHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(Database.CreateDB.CASH, cashORcard);
        values.put(Database.CreateDB.AMOUNT, amount);
        values.put(Database.CreateDB.CATEGORY, category);
        values.put(Database.CreateDB.DETAIL, detail);
        values.put(Database.CreateDB.firstCash, cashValue);
        values.put(Database.CreateDB.firstAccount, AccountVal);
        values.put(Database.CreateDB.allMoney, money);
        return mDB.insert(Database.CreateDB.TABLENAME, null, values);
    }
于 2013-04-17T06:03:23.407 回答
0
public static final String AMOUNT="0"

这应该是

public static final String AMOUNT="AMOUNT"

您尝试插入名为“0”的列的结果

于 2013-04-17T04:26:00.257 回答
0

看到错误是0=567。在这里,您的列名是integer类型。所以这是错误的。列名应该是有效的。所以只需尝试以下

public static final String CREATE=
        "CREATE TABLE "+TABLENAME
        +" (_id integer primary key autoincrement, "
        + CASH +" text not null , "
        + AMOUNT+" text not null , "
        + CATEGORY+" text not null , "
        + DETAIL+" text , "
        + firstCash+" text not null , "
        + firstAccount+" text not null , "
        + allMoney+" text not null );";  

而不是这个

public static final String CREATE=
        "CREATE TABLE "+TABLENAME
        +" ( _id integer primary key autoincrement, "
        +" CASH text not null , "
        +" AMOUNT text not null , "
        +" CATEGORY text not null , "
        +" DETAIL text , "
        +" firstCash text not null , "
        +" firstAccount text not null , "
        +" allMoney text not null );";  

也改变你的

firstCash,firstAccount,allMoney,AMOUNT varibales values to be valid string. 

喜欢

public static String firstCash="firstCash";
public static String firstAccount="firstAccount";
public static String allMoney="allMoney";
public static final String AMOUNT="AMOUNT";

为什么因为列名应该是有效的。

我希望这能帮到您。

于 2013-04-17T05:14:17.780 回答