Android数据库插入行

时间:2011-08-02 07:09:00

标签: android sqlite

我已经编写了动态插入部分。当我执行时他将insert语句插入到sqlite浏览器中,其插入正常。如果通过代码执行则存在一些问题:

这是我的代码:

  public void insertTableRecord(String strTableName, String strToFields, String strValues){
    DBAdapter dbAdapter = DBAdapter.getDBAdapterInstance(DownlaodTableActivity.this);
    dbAdapter.openDataBase();

    ContentValues initialValues = new ContentValues();
    initialValues.put(strToFields,strValues);
    long n = dbAdapter.insertRecordsInDB(strTableName, null, initialValues);
    System.out.println( " -- inserted status : --- " + n);
}

sql语句是:

        INSERT INTO  WMTransactionControl(ExecutiveCode,FreeIssuePrefix,DisPaySchedulePrefix,NextFreeIssueNo,NextReturnNo,UploadedType,DisNextFreeIssueNo,DisNextFOCNo,NextVisitNo,DisNextOrderNo,UploadedOn,NextReturnAcceptNo,BusinessUnit,TXNReferencePrefix,OrderPrefix,UploadedMethod,FOCPrefix,ReturnPrefix,RetailerPrefix,NextRetailerNo,NextInvoiceNo,NextGRNNo,InvoicePrefix,NextTXNReference,NextOrderNo,ReturnAcceptPrefix,PaySchedulePrefix,NextReceiptNo,NextFOCNo,NextPayScheduleNo,NextGRONo,DisReturnPrefix,DisReceiptPrefix,DisNextReturnNo,DisOrderPrefix,DisNextReceiptNo,DisNextPayScheduleNo,NextActivityNo,DisInvoicePrefix,DisNextInvoiceNo,UploadedBy,DisFreeIssuePrefix,DisFOCPrefix,ReceiptPrefix) VALUES("WAT2 "," ","","1","20","1","1","1","15","1","Jun 17 2011 6:33PM","1","HEMA","20110708 ","OR4 ","3"," ","RT4 ","TEM4","10","1","1","IN4 ","2","37"," ","PS4","1","20","41","1"," "," ","1"," ","1","1","1"," ","1","WAT2"," "," ","RP4 ");

这是我通过sqlite浏览器运行的sql语句。没关系。 但是代码 错误的说法是这样的:

     08-02 12:25:35.447: ERROR/Database(304): Error inserting  ExecutiveCode,FreeIssuePrefix,DisPaySchedulePrefix,NextFreeIssueNo,NextReturnNo,UploadedType,DisNextFreeIssueNo,DisNextFOCNo,NextVisitNo,DisNextOrderNo,UploadedOn,NextReturnAcceptNo,BusinessUnit,TXNReferencePrefix,OrderPrefix,UploadedMethod,FOCPrefix,ReturnPrefix,RetailerPrefix,NextRetailerNo,NextInvoiceNo,NextGRNNo,InvoicePrefix,NextTXNReference,NextOrderNo,ReturnAcceptPrefix,PaySchedulePrefix,NextReceiptNo,NextFOCNo,NextPayScheduleNo,NextGRONo,DisReturnPrefix,DisReceiptPrefix,DisNextReturnNo,DisOrderPrefix,DisNextReceiptNo,DisNextPayScheduleNo,NextActivityNo,DisInvoicePrefix,DisNextInvoiceNo,UploadedBy,DisFreeIssuePrefix,DisFOCPrefix,ReceiptPrefix="WAT2 "," ","","1","20","1","1","1","15","1","Jun 17 2011 6:33PM","1","HEMA","20110708 ","OR4 ","3"," ","RT4 ","TEM4","10","1","1","IN4 ","2","37"," ","PS4","1","20","41","1"," "," ","1"," ","1","1","1"," ","1","WAT2"," "," ","RP4 "
     08-02 12:25:35.447: ERROR/Database(304): android.database.sqlite.SQLiteException: 1 values for 44 columns: , while compiling: INSERT INTO WMTransactionControl(ExecutiveCode,FreeIssuePrefix,DisPaySchedulePrefix,NextFreeIssueNo,NextReturnNo,UploadedType,DisNextFreeIssueNo,DisNextFOCNo,NextVisitNo,DisNextOrderNo,UploadedOn,NextReturnAcceptNo,BusinessUnit,TXNReferencePrefix,OrderPrefix,UploadedMethod,FOCPrefix,ReturnPrefix,RetailerPrefix,NextRetailerNo,NextInvoiceNo,NextGRNNo,InvoicePrefix,NextTXNReference,NextOrderNo,ReturnAcceptPrefix,PaySchedulePrefix,NextReceiptNo,NextFOCNo,NextPayScheduleNo,NextGRONo,DisReturnPrefix,DisReceiptPrefix,DisNextReturnNo,DisOrderPrefix,DisNextReceiptNo,DisNextPayScheduleNo,NextActivityNo,DisInvoicePrefix,DisNextInvoiceNo,UploadedBy,DisFreeIssuePrefix,DisFOCPrefix,ReceiptPrefix) VALUES(?);
     08-02 12:25:35.447: ERROR/Database(304):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
     08-02 12:25:35.447: ERROR/Database(304):     at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
     08-02 12:25:35.447: ERROR/Database(304):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
     08-02 12:25:35.447: ERROR/Database(304):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
     08-02 12:25:35.447: ERROR/Database(304):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:36)
     08-02 12:25:35.447: ERROR/Database(304):     at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1145)
     08-02 12:25:35.447: ERROR/Database(304):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1536)
     08-02 12:25:35.447: ERROR/Database(304):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)
     08-02 12:25:35.447: ERROR/Database(304):     at com.xont.db.DBAdapter.insertRecordsInDB(DBAdapter.java:214)
     08-02 12:25:35.447: ERROR/Database(304):     at com.xont.controller.DownlaodTableActivity.insertTableRecord(DownlaodTableActivity.java:325)
     08-02 12:25:35.447: ERROR/Database(304):     at com.xont.controller.DownlaodTableActivity.loadDownloadData(DownlaodTableActivity.java:264)
     08-02 12:25:35.447: ERROR/Database(304):     at com.xont.controller.DownlaodTableActivity.onOptionsItemSelected(DownlaodTableActivity.java:204)
     08-02 12:25:35.447: ERROR/Database(304):     at android.app.Activity.onMenuItemSelected(Activity.java:2195)
     08-02 12:25:35.447: ERROR/Database(304):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:730)
      08-02 12:25:35.447: ERROR/Database(304):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
     08-02 12:25:35.447: ERROR/Database(304):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
     08-02 12:25:35.447: ERROR/Database(304):     at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
     08-02 12:25:35.447: ERROR/Database(304):     at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
     08-02 12:25:35.447: ERROR/Database(304):     at android.view.View$PerformClick.run(View.java:8816)
     08-02 12:25:35.447: ERROR/Database(304):     at android.os.Handler.handleCallback(Handler.java:587)
     08-02 12:25:35.447: ERROR/Database(304):     at android.os.Handler.dispatchMessage(Handler.java:92)
     08-02 12:25:35.447: ERROR/Database(304):     at android.os.Looper.loop(Looper.java:123)
     08-02 12:25:35.447: ERROR/Database(304):     at android.app.ActivityThread.main(ActivityThread.java:4627)
     08-02 12:25:35.447: ERROR/Database(304):     at java.lang.reflect.Method.invokeNative(Native Method)
     08-02 12:25:35.447: ERROR/Database(304):     at java.lang.reflect.Method.invoke(Method.java:521)
    08-02 12:25:35.447: ERROR/Database(304):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    08-02 12:25:35.447: ERROR/Database(304):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    08-02 12:25:35.447: ERROR/Database(304):     at dalvik.system.NativeStart.main(Native Method)

strToFields = ExecutiveCode,FreeIssuePrefix,DisPaySchedulePrefix,NextFreeIssueNo,NextReturnNo,UploadedType,DisNextFreeIssueNo,DisNextFOCNo,NextVisitNo,DisNextOrderNo,UploadedOn,NextReturnAcceptNo,BusinessUnit,TXNReferencePrefix,OrderPrefix,UploadedMethod,FOCPrefix,ReturnPrefix,RetailerPrefix,NextRetailerNo,NextInvoiceNo,NextGRNNo,InvoicePrefix ,NextTXNReference,NextOrderNo,ReturnAcceptPrefix,PaySchedulePrefix,NextReceiptNo,NextFOCNo,NextPayScheduleNo,NextGRONo,DisReturnPrefix,DisReceiptPrefix,DisNextReturnNo,DisOrderPrefix,DisNextReceiptNo,DisNextPayScheduleNo,NextActivityNo,DisInvoicePrefix,DisNextInvoiceNo,UploadedBy,DisFreeIssuePrefix,DisFOCPrefix,ReceiptPrefix

strValues =“WAT2”,“”,“”,“1”,“20”,“1”,“1”,“1”,“15”,“1”,“2011年6月17日下午6:33 “,”1“,”HEMA“,”20110708“,”OR4“,”3“,”“,”RT4“,”TEM4“,”10“,”1“,”1“,”IN4“,” 2“,”37“,”“,”PS4“,”1“,”20“,”41“,”1“,”“,”“,”1“,”“,”1“,”1“ ,“1”,“”,“1”,“WAT2”,“”,“”,“RP4”

请帮助我。

提前致谢..

2 个答案:

答案 0 :(得分:1)

您必须将所有列值设为initialValues.put(strToFields,strValues); 你写的只给出了一个列的值。你在表中有其他列。请把它们放在这样的

int totalColumn=50;
String columName[]=new String[totalColumn];
//dynamically generating columnName;
String values[]=new String[totalColumn];

//动态生成值

for(int i=0;i<totalColumn;i++)
 initialValues.put(columnName[i],values[i]);`

答案 1 :(得分:0)

这是你必须这样做的方式

ContentValues initialValues = new ContentValues();
initialValues.put("ExecutiveCode","WAT2 ");
initialValues.put("FreeIssuePrefix"," ");
initialValues.put("DisPaySchedulePrefix","");
initialValues.put("NextFreeIssueNo","1");
initialValues.put("NextReturnNo","20");
initialValues.put("UploadedType","1");
//AND SO ON

每个值必须单独放入ContentValues