Search code examples
androidsqlitespinnerselectedvalue

write spinner selected value to database


i've the following code, that selects the value from spinner and should write to new table (tbl_trunk)..i used the following code but didn't work...

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

insert method is:

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)

Solution

  • You create a separate db class as follows with your own parameters:

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

    in activity class remove all db related queries and add

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