English 中文(简体)
2. 书面先天选择数据库价值
原标题:write spinner selected value to database

i 删除以下代码,即从胎盘中挑选数值,并写到新表格(tbl_trunk.i 使用了以下代码,但做了t工作......

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(); 
                }

                } 

how should i add the selected row to new table?? the error log is

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)
最佳回答

1. 您按照自己的参数单独设立一门小类:

  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();

    }}

在活动类别中删除所有与干 and有关的查询和增加

DataBaseHelper dbHelper;  
  this.dbHelper=new DataBaseHelper(YOURACTIVITYCLASS.this);   
  dbHelper.insert("121", "some text111");
问题回答

无需再造桌子。 在该类别内设立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();

}




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签