0

我在我的 android 应用程序中有一个插入条目逻辑,它在我上次访问该应用程序之前一直响应完美。但它突然开始停止响应和运行。所以请帮助我在我的逻辑中犯了什么错误或错误,或者我的编码中还有什么问题会突然响应 sqlite 以这种方式运行。请尽快帮助我,因为我今天对 android-application 进行了最后的演示,而我被排除在外的时间非常少。

在此先感谢您的帮助。

问候 :)

按照我的创建表逻辑:

private static final int DATABASE_VERSION = 1; // db version
    private static final String DATABASE_NAME = "db_Blood_Donation"; // db name
    private static final String TABLE_NAME = "Table1"; // table name
    // table fields
    private static final String KEY_ID = "bid";
    private static final String KEY_NAME = "bname";
    private static final String KEY_DATE = "bdate";
    private static final String KEY_GRP = "bgroup";
    private static final String KEY_ADD = "baddress";
    private static final String KEY_PH = "bphno";

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        /* ------------- CREATE TABLE ------------- */


        String CREATE_TABLE1 = "CREATE TABLE " + TABLE_NAME + "(" + KEY_ID
                + "INTEGER PRIMARY KEY, " + KEY_NAME + "TEXT, " + KEY_DATE
                + "TEXT, " + KEY_GRP + "TEXT, " + KEY_ADD + "TEXT, " + KEY_PH
                + "TEXT " + ")";

        db.execSQL(CREATE_TABLE1);
        Log.d("Table_Created", "table1");
    }

以下是我的 AddEntry 逻辑:

/* ----------------- ADD USER ------------- */

    void addUser(BDFieldClass user) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(KEY_NAME, user.getBname());
        cv.put(KEY_DATE, user.getBdate());
        cv.put(KEY_GRP, user.getBgroup());
        cv.put(KEY_ADD, user.getBaddress());
        cv.put(KEY_PH, user.getPhno());

        // INSERT ROWS

        db.insertOrThrow(TABLE_NAME, null, cv);
        db.close();
    }

以下是我调用插入逻辑的代码:

/*-------------------------------- 添加记录 --------------- ----------------- */

    btnadd.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
    EditText ednm = (EditText)findViewById(R.id.txtname);
        String name = ednm.getText().toString();

        eddate = (EditText)findViewById(R.id.txtdate);
        String date1 = eddate.getText().toString();

        EditText edadd = (EditText)findViewById(R.id.txtadd);
        String add = edadd.getText().toString();

        EditText edno = (EditText)findViewById(R.id.txtno);
        String phno = edno.getText().toString();


    dbh.addUser(new BDFieldClass(name,date1,bgrp,add,phno));

以下是 LogCat 错误:

09-01 06:20:59.087: W/KeyCharacterMap(359): No keyboard for id 0
09-01 06:20:59.097: W/KeyCharacterMap(359): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
09-01 06:21:16.307: D/dalvikvm(359): GC_FOR_MALLOC freed 4630 objects / 232480 bytes in 119ms
09-01 06:21:30.917: D/Table_Created(359): table1
09-01 06:21:30.936: I/Database(359): sqlite returned: error code = 1, msg = table Table1 has no column named bname
09-01 06:21:30.947: D/AndroidRuntime(359): Shutting down VM
09-01 06:21:30.947: W/dalvikvm(359): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
09-01 06:21:30.976: E/AndroidRuntime(359): FATAL EXCEPTION: main
09-01 06:21:30.976: E/AndroidRuntime(359): android.database.sqlite.SQLiteException: table Table1 has no column named bname: , while compiling: INSERT INTO Table1(bname, bphno, bgroup, bdate, baddress) VALUES(?, ?, ?, ?, ?);
09-01 06:21:30.976: E/AndroidRuntime(359):  at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
09-01 06:21:30.976: E/AndroidRuntime(359):  at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
09-01 06:21:30.976: E/AndroidRuntime(359):  at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
09-01 06:21:30.976: E/AndroidRuntime(359):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
09-01 06:21:30.976: E/AndroidRuntime(359):  at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:36)
09-01 06:21:30.976: E/AndroidRuntime(359):  at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1145)
09-01 06:21:30.976: E/AndroidRuntime(359):  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1536)
09-01 06:21:30.976: E/AndroidRuntime(359):  at android.database.sqlite.SQLiteDatabase.insertOrThrow(SQLiteDatabase.java:1432)
09-01 06:21:30.976: E/AndroidRuntime(359):  at com.example.blooddonationapp.DBHelper.addUser(DBHelper.java:66)
09-01 06:21:30.976: E/AndroidRuntime(359):  at com.example.blooddonationapp.CreateClass$2.onClick(CreateClass.java:80)
09-01 06:21:30.976: E/AndroidRuntime(359):  at android.view.View.performClick(View.java:2408)
09-01 06:21:30.976: E/AndroidRuntime(359):  at android.view.View$PerformClick.run(View.java:8816)
09-01 06:21:30.976: E/AndroidRuntime(359):  at android.os.Handler.handleCallback(Handler.java:587)
09-01 06:21:30.976: E/AndroidRuntime(359):  at android.os.Handler.dispatchMessage(Handler.java:92)
09-01 06:21:30.976: E/AndroidRuntime(359):  at android.os.Looper.loop(Looper.java:123)
09-01 06:21:30.976: E/AndroidRuntime(359):  at android.app.ActivityThread.main(ActivityThread.java:4627)
09-01 06:21:30.976: E/AndroidRuntime(359):  at java.lang.reflect.Method.invokeNative(Native Method)
09-01 06:21:30.976: E/AndroidRuntime(359):  at java.lang.reflect.Method.invoke(Method.java:521)
09-01 06:21:30.976: E/AndroidRuntime(359):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-01 06:21:30.976: E/AndroidRuntime(359):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-01 06:21:30.976: E/AndroidRuntime(359):  at dalvik.system.NativeStart.main(Native Method)
09-01 06:21:33.286: I/Process(359): Sending signal. PID: 359 SIG: 9
4

2 回答 2

4

使用以下字符串创建表。注意在列类型开头添加空格

   String CREATE_TABLE1 = "CREATE TABLE " + TABLE_NAME + "(" + KEY_ID
            + " INTEGER PRIMARY KEY, " + KEY_NAME + " TEXT, " + KEY_DATE
            + " TEXT, " + KEY_GRP + " TEXT, " + KEY_ADD + " TEXT, " + KEY_PH
            + " TEXT " + ")";
于 2012-09-01T01:07:32.403 回答
1

问题出在这里:KEY_NAME + "TEXT, ". 它将创建一个列:bnameTEXT在您的数据库中!您应该在 之前添加一个空格TEXT

于 2012-09-01T01:27:59.890 回答