android sqlite中的自动增量ID为pimary key / logcat错误

时间:2014-12-09 16:04:07

标签: android sqlite

您好我是Android新手我试图在我的SQLite数据库上添加id作为主键自动增量,但是log-cat错误消息说" table Expenses有5列,但提供了4个值"我不希望ID显示在用户界面上。有人可以帮我吗?感谢。

这是我的源代码:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //spinner 
        mCategory = (Spinner)findViewById(R.id.spCategory);
        SpPayType = (Spinner)findViewById(R.id.sp_PayType);

        adapter = ArrayAdapter.createFromResource(this, R.array.Category, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        mCategory.setAdapter(adapter);

        adapter = ArrayAdapter.createFromResource(this, R.array.PType, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        SpPayType.setAdapter(adapter);

        db=openOrCreateDatabase("MyDB2",MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS Expenses(ID INTEGER PRIMARY KEY AUTOINCREMENT,Category String,Amount int,PType String,Date VARCHAR);");
    }
    /*function to add data in to db */

    public void CreatExpenses(View view)
    {
       //  EditText edittext6 = (EditText)findViewById(R.id.edittrans_id);
          mCategory          =  (Spinner  )findViewById(R.id.spCategory);
          EditText edittext2 = (EditText )findViewById(R.id.AmountEditText);
          SpPayType          = (Spinner  )findViewById(R.id.sp_PayType);
          EditText edittext4 = (EditText )findViewById(R.id.DateEditText);


        // ID                =  edittext6.getText().toString();
          category           =        mCategory.getSelectedItem().toString();
          Amount             =  edittext2.getText().toString();
          payment_type       =  SpPayType.getSelectedItem().toString();
          Date               =  edittext4.getText().toString();


          db.execSQL("INSERT INTO  Expenses VALUES('"+category+"','"+Amount+"','"+payment_type+"','"+Date+"');");

          Toast.makeText(getBaseContext(), "Your info is saved successfully!", Toast.LENGTH_SHORT).show();
    }

    public void ShowExpens(View view)
        {

        Cursor c=db.rawQuery("SELECT * from Expenses  ", null);
        int count= c.getCount();
        c.moveToFirst();
        TableLayout tableLayout = new TableLayout(getApplicationContext());
        tableLayout.setVerticalScrollBarEnabled(true);
        TableRow tableRow;
        TextView  TransID,ExpeCategory,ExpAmount,PayMethod,ExpDate;
        tableRow = new TableRow(getApplicationContext());

    /*    TransID = new TextView(getApplicationContext());
        TransID.setText("ID");
        TransID.setTextColor(Color.RED);
        TransID.setTypeface(null, Typeface.BOLD);
        TransID.setPadding(10, 10, 10, 10);
        tableRow.addView(TransID); 
*/      
        ExpeCategory=new TextView(getApplicationContext());
        ExpeCategory.setText("Category");
        ExpeCategory.setTextColor(Color.RED);
        ExpeCategory.setTypeface(null, Typeface.BOLD);
        ExpeCategory.setPadding(10, 10, 10, 10);
        tableRow.addView(ExpeCategory);



        ExpAmount = new TextView(getApplicationContext());
        ExpAmount.setText("Amount");
        ExpAmount.setTextColor(Color.GREEN);
        ExpAmount.setTypeface(null, Typeface.BOLD);
        ExpAmount.setPadding(10, 10, 10, 10);
        tableRow.addView(ExpAmount);

        PayMethod = new TextView(getApplicationContext());
        PayMethod.setText("PType");
        PayMethod.setTextColor(Color.RED);
        PayMethod.setTypeface(null, Typeface.BOLD);
        PayMethod.setPadding(10, 10, 10, 10);
        tableRow.addView(PayMethod);

        ExpDate = new TextView(getApplicationContext());
        ExpDate.setText("Date");
        ExpDate.setTextColor(Color.RED);
        ExpDate.setTypeface(null, Typeface.BOLD);
        ExpDate.setPadding(10, 10, 10, 10);
        tableRow.addView(ExpDate);

       tableLayout.addView(tableRow);

         for (Integer j = 0; j < count; j++)
            {
             tableRow = new TableRow(getApplicationContext());
             TransID = new TextView(getApplicationContext());
             TransID.setText(c.getString(c.getColumnIndex("ID")));

             ExpeCategory = new TextView(getApplicationContext());
             ExpeCategory.setText(c.getString(c.getColumnIndex("Category")));

             ExpAmount = new TextView(getApplicationContext());
             ExpAmount.setText(c.getString(c.getColumnIndex("Amount")));

             PayMethod = new TextView(getApplicationContext());
             PayMethod.setText(c.getString(c.getColumnIndex("PType")));
             ExpDate = new TextView(getApplicationContext());
             ExpDate.setText(c.getString(c.getColumnIndex("Date")));


           /*  TransID.setPadding(10, 10, 10, 10);
             ExpeCategory.setPadding(10, 10, 10, 10);
             ExpAmount.setPadding(10, 10, 10, 10);
             PayMethod.setPadding(10, 10, 10, 10);
             ExpDate.setPadding(10, 10, 10, 10);
            */
       //      tableRow.addView(TransID);
             tableRow.addView(ExpeCategory);
             tableRow.addView(ExpAmount);
             tableRow.addView(PayMethod);
             tableRow.addView(ExpDate);


             tableLayout.addView(tableRow);
             c.moveToNext() ;
         }
         setContentView(tableLayout);
    db.close();
    }
    public void close(View view)
    {
        System.exit(0);
    }






    }

以下是log-cat

12-09 11:00:19.604: I/Choreographer(1161): Skipped 34 frames!  The application may be doing too much work on its main thread.
12-09 11:00:19.714: D/gralloc_goldfish(1161): Emulator without GPU emulation detected.
12-09 11:00:33.394: E/SQLiteLog(1161): (1) table Expenses has 5 columns but 4 values were supplied
12-09 11:00:33.394: D/AndroidRuntime(1161): Shutting down VM
12-09 11:00:33.404: W/dalvikvm(1161): threadid=1: thread exiting with uncaught exception (group=0xb2ac0ba8)
12-09 11:00:33.534: E/AndroidRuntime(1161): FATAL EXCEPTION: main
12-09 11:00:33.534: E/AndroidRuntime(1161): Process: com.info.househouldexpcalulator, PID: 1161
12-09 11:00:33.534: E/AndroidRuntime(1161): java.lang.IllegalStateException: Could not execute method of the activity
12-09 11:00:33.534: E/AndroidRuntime(1161):     at android.view.View$1.onClick(View.java:3823)
12-09 11:00:33.534: E/AndroidRuntime(1161):     at android.view.View.performClick(View.java:4438)
12-09 11:00:33.534: E/AndroidRuntime(1161):     at android.view.View$PerformClick.run(View.java:18422)
12-09 11:00:33.534: E/AndroidRuntime(1161):     at android.os.Handler.handleCallback(Handler.java:733)
12-09 11:00:33.534: E/AndroidRuntime(1161):     at android.os.Handler.dispatchMessage(Handler.java:95)
12-09 11:00:33.534: E/AndroidRuntime(1161):     at android.os.Looper.loop(Looper.java:136)
12-09 11:00:33.534: E/AndroidRuntime(1161):     at android.app.ActivityThread.main(ActivityThread.java:5017)
12-09 11:00:33.534: E/AndroidRuntime(1161):     at java.lang.reflect.Method.invokeNative(Native Method)
12-09 11:00:33.534: E/AndroidRuntime(1161):     at java.lang.reflect.Method.invoke(Method.java:515)
12-09 11:00:33.534: E/AndroidRuntime(1161):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-09 11:00:33.534: E/AndroidRuntime(1161):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-09 11:00:33.534: E/AndroidRuntime(1161):     at dalvik.system.NativeStart.main(Native Method)
12-09 11:00:33.534: E/AndroidRuntime(1161): Caused by: java.lang.reflect.InvocationTargetException
12-09 11:00:33.534: E/AndroidRuntime(1161):     at java.lang.reflect.Method.invokeNative(Native Method)
12-09 11:00:33.534: E/AndroidRuntime(1161):     at java.lang.reflect.Method.invoke(Method.java:515)
12-09 11:00:33.534: E/AndroidRuntime(1161):     at android.view.View$1.onClick(View.java:3818)
12-09 11:00:33.534: E/AndroidRuntime(1161):     ... 11 more
12-09 11:00:33.534: E/AndroidRuntime(1161): Caused by: android.database.sqlite.SQLiteException: table Expenses has 5 columns but 4 values were supplied (code 1): , while compiling: INSERT INTO  Expenses VALUES('Foods','10','Cash','11/12/2014');
12-09 11:00:33.534: E/AndroidRuntime(1161):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
12-09 11:00:33.534: E/AndroidRuntime(1161):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
12-09 11:00:33.534: E/AndroidRuntime(1161):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
12-09 11:00:33.534: E/AndroidRuntime(1161):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
12-09 11:00:33.534: E/AndroidRuntime(1161):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
12-09 11:00:33.534: E/AndroidRuntime(1161):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
12-09 11:00:33.534: E/AndroidRuntime(1161):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
12-09 11:00:33.534: E/AndroidRuntime(1161):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
12-09 11:00:33.534: E/AndroidRuntime(1161):     at com.info.househouldexpcalulator.MainActivity.CreatExpenses(MainActivity.java:71)
12-09 11:00:33.534: E/AndroidRuntime(1161):     ... 14 more
12-09 11:00:37.444: I/Process(1161): Sending signal. PID: 1161 SIG: 9

2 个答案:

答案 0 :(得分:-1)

CL是正确的以前的答案被打破了 我将您的创建 sql 更改为
“CREATE TABLE IF NOT NOT EXISTS'Expenses'(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,Category String,Amount int,PType String,Date VARCHAR);”

然后将 sql 插入

INSERT INTO费用(类别,金额,PType,日期)VALUES('“+ category +”','“+ Amount +”','“+ payment_type +”','“+ Date +”');

'我不希望ID显示在用户界面上' 这应该对用户界面要求没有影响

答案 1 :(得分:-1)

创建ID列,如 INTEGER PRIMARY KEY AUTOINCREMENT ,您的问题就会得到解决。

CREATE TABLE MyTable ('_id' INTEGER PRIMARY KEY AUTOINCREMENT, c2 text, c3 text, c4 int, c5 text, c6 text, c7 int);

之后不需要在insert语句中包含_id,SQL将收到_id为NULL并自动处理为你设置_id。