1

我有以下代码,它从微调器中选择值并应该写入新表(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)
4

2 回答 2

2

您使用自己的参数创建一个单独的数据库类,如下所示:

  public class DataBaseHelper  extends SQLiteOpenHelper{
private static final String DB_NAME="SampleMsgDb";
private static final String TABLE="SampleTABLE";
private static final int DB_VERSION=1;
private static final String COLUMN1="received_Time";
private static final String COLUMN2="col1";
private static final String COLUMN3="col2";
private  Context myContext;
private SQLiteDatabase myDataBase;
DataBaseHelper dbHelper;

public DataBaseHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
    myContext=context;
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    String sql = "create table " + TABLE + "( "+ COLUMN1 + " integer , " + COLUMN2 + " text not null, "
            + COLUMN3 + " text not null);";
    Log.d("EventsData", "onCreate: " + sql);
    db.execSQL(sql);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    if (oldVersion >= newVersion)
        return;

    String sql = null;
    if (oldVersion == 1) 
        sql = "alter table " + TABLE + " add note text;";
    if (oldVersion == 2)
        sql = "";

    Log.d("EventsData", "onUpgrade  : " + sql);
    if (sql != null)
        db.execSQL(sql);
}
public void insert(String number, String message) {
    dbHelper=new DataBaseHelper(myContext);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DataBaseHelper.COLUMN1, System.currentTimeMillis());
        values.put(DataBaseHelper.COLUMN2, number);
        values.put(DataBaseHelper.COLUMN3, message);
        db.insert(DataBaseHelper.TABLE, null, values);
        db.close();
        if (myDataBase != null)
            myDataBase.close();

      }

 @Override
    public synchronized void close() {
      super.close();
        if (myDataBase != null)
            myDataBase.close();

    }}

在活动类中删除所有与数据库相关的查询并添加

DataBaseHelper dbHelper;  
  this.dbHelper=new DataBaseHelper(YOURACTIVITYCLASS.this);   
  dbHelper.insert("121", "some text111");
于 2012-05-02T13:43:45.797 回答
1

无需一次又一次地创建表。创建1个数据库助手类,在类内部创建表并编写1个插入方法并在活动中调用该插入方法..

public void insert(String name) {

    dbHelper = new TransactionDetailsHelper(myContext);
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("name", name);
    db.insert("SAMPLE_TABLE_TRUNK", null, values);
    db.close();
    if (myDataBase != null)
        myDataBase.close();

}
于 2012-05-02T13:20:10.597 回答