Android database insert row
I have written dynamic insert part.When I excute he Insert statement into sqlite browser , its inserting fine.If do throuh code there are some issues:
This is my code:
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 statement is :
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 ");
This is sql statement i ran through sqlite browser. its ok. but code Error saying like this :
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)
开发者_C百科 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","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 "
Please Help me this.
Thanks in advance..
You have to put all column values like initialValues.put(strToFields,strValues);
what you wrote gives only the value for a column.you have other column in the table.Pls put them like this
int totalColumn=50;
String columName[]=new String[totalColumn];
//dynamically generating columnName;
String values[]=new String[totalColumn];
//dynamically generating values
for(int i=0;i<totalColumn;i++)
initialValues.put(columnName[i],values[i]);`
This is how you must do it
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
Each value must be put into the ContentValues alone
精彩评论