我有以下代码,它从微调器中选择值并应该写入新表(tbl_trunk
)..我使用了以下代码但没有工作......
myDB.execSQL("CREATE TABLE " + SAMPLE_TABLE_NAME + " (" + _id
+ " INTEGER PRIMARY KEY AUTOINCREMENT , " + cust_name
+ " TEXT , " + cust_add + " TEXT)");
myDB.execSQL("insert into tbl_customer(cust_name, cust_add) values ('Fool', 'FF' );");
Cursor c = myDB.query(SAMPLE_TABLE_NAME, null, null, null, null, null, null);
char cust_nameColumnIndex = (char) c.getColumnIndexOrThrow("cust_name");
char cust_addColumnIndex = (char) c.getColumnIndexOrThrow("cust_add");
adapterForSpinner = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item);
adapterForSpinner
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
String selection;
spinner.setAdapter(adapterForSpinner);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@SuppressWarnings("null")
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// TODO Auto-generated method stub
SQLiteDatabase myDB = null;
Toast.makeText(
parent.getContext(),
"Customer is "
+ parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_LONG).show();
String selected = parent.getItemAtPosition(pos).toString();
myDB.execSQL("CREATE TABLE " + SAMPLE_TABLE_TRUNK + " (" + _id
+ " INTEGER PRIMARY KEY AUTOINCREMENT , " + cust_name
+ " TEXT)");
myDB.execSQL("insert into tbl_trunk(cust_name) values (pos);");
insert(selected);
插入方法是:
public void insert(String cust_name) {
String SAMPLE_DB_NAME = "db_sales";
dbHelper = new DatabaseHelper(getBaseContext());
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("cust_name", cust_name);
db.insert("SAMPLE_TABLE_TRUNK", null, values);
db.close();
if (SAMPLE_DB_NAME != null)
{
// SAMPLE_DB_NAME.close();
}
}
我应该如何将选定的行添加到新表?错误日志是
05-02 19:27:45.048: E/Database(3668): Error inserting cust_name=Ali - AA
05-02 19:27:45.048: E/Database(3668): android.database.sqlite.SQLiteException: no such table: SAMPLE_TABLE_TRUNK: , while compiling: INSERT INTO SAMPLE_TABLE_TRUNK(cust_name) VALUES(?);
05-02 19:27:45.048: E/Database(3668): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
05-02 19:27:45.048: E/Database(3668): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
05-02 19:27:45.048: E/Database(3668): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
05-02 19:27:45.048: E/Database(3668): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
05-02 19:27:45.048: E/Database(3668): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:36)
05-02 19:27:45.048: E/Database(3668): at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1145)
05-02 19:27:45.048: E/Database(3668): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1536)
05-02 19:27:45.048: E/Database(3668): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)
05-02 19:27:45.048: E/Database(3668): at numair.tab.layout.Customers$1.insert(Customers.java:117)
05-02 19:27:45.048: E/Database(3668): at numair.tab.layout.Customers$1.onItemSelected(Customers.java:87)
05-02 19:27:45.048: E/Database(3668): at android.widget.AdapterView.fireOnSelected(AdapterView.java:864)