0

I'm trying to insert data to sqlite database in android. I'm new to android sqlite. I'm getting this error on the LogCat output:

.09-10 09:55:38.855: E/SQLiteLog(7735): (1) table lots_farmertoagentcollection has no column named entrydatetime

This is my code

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mButt=(Button)findViewById(R.id.button1);

        mDSLCode="a1";

        mButt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                submitData();
            }

        });

    }
private void submitData()
    {
        try {


        String VndCode=mFarmer_codetxt.getText().toString();
        String ltrs=mLtrs.getText().toString();
        String metrolac=mtext.getText().toString();
        final String mdrq=mDRQ.getText().toString();
        Calendar c = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String submitedDate = df.format(c.getTime());



        insertLC(VndCode, ltrs, metrolac, mdrq, mDSLCode, submitedDate, null, null);

        DbBackup.copyDatabase(getApplicationContext());


        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    public void insertLC(String vendorcode,String lotsquantity,String metrolacreading,String drq,String dslprogrammecode,String entrydatetime,String remarks,String status) {
        // TODO Auto-generated method stub
        mDbA= new DBAdapter(this);
        mDbA.open();
        try {
            long i = mDbA.insertLC(vendorcode, lotsquantity, metrolacreading, drq, dslprogrammecode, entrydatetime, remarks, status);
            if(i != -1){
        Toast.makeText(MainActivity.this, "Data Recorded",Toast.LENGTH_LONG).show();
            }
        } catch (SQLException e) {
            Toast.makeText(MainActivity.this, "Some problem occurred",
                    Toast.LENGTH_LONG).show();

        }
        mDbA.close();
    }

DBA Adapter class

public class DBAdapter 
{
private static final String FARMER_TO_AGENT_COLLECTION_TABLE = "lots_farmertoagentcollection";
    public static final String FARMER_TO_AGENT_KEY_FA_ID = "fa_id";
    public static final String FARMER_TO_AGENT_KEY_VC= "vendorcode";
    public static final String FARMER_TO_AGENT_KEY_LQ= "lotsquantity";
    public static final String FARMER_TO_AGENT_KEY_MR= "metrolacreading";
    public static final String FARMER_TO_AGENT_KEY_DRQ= "drq";
    public static final String FARMER_TO_AGENT_KEY_DPC= "dslprogrammecode";
    public static final String FARMER_TO_AGENT_KEY_EDT= "entrydatetime";
    public static final String FARMER_TO_AGENT_KEY_RE= "remarks";
    public static final String FARMER_TO_AGENT_KEY_STS= "status";

public long insertLC(String vendorcode,String lotsquantity,String metrolacreading,String drq,String dslprogrammecode,String entrydatetime,String remarks,String status)
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(FARMER_TO_AGENT_KEY_VC, vendorcode);
        initialValues.put(FARMER_TO_AGENT_KEY_LQ, lotsquantity);
        initialValues.put(FARMER_TO_AGENT_KEY_MR, metrolacreading);
        initialValues.put(FARMER_TO_AGENT_KEY_DRQ, drq);
        initialValues.put(FARMER_TO_AGENT_KEY_DPC, dslprogrammecode);
        initialValues.put(FARMER_TO_AGENT_KEY_EDT, entrydatetime);
        initialValues.put(FARMER_TO_AGENT_KEY_RE, remarks);
        initialValues.put(FARMER_TO_AGENT_KEY_STS, status);

        return  mDb.insert(FARMER_TO_AGENT_COLLECTION_TABLE, null, initialValues);

    }
}

DBHelper class

public class DBHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "NEW.db";
    private static final int DATABASE_VERSION = 1;

    private static final String CONFIGURATIONS_TABLE = "configurations";
    public static final String CONFIG_KEY_ID = "id";
    public static final String CONFIG_KEY_KEY = "token";
    public static final String CONFIG_KEY_VALUE= "val";

    private static final String FARMER_TO_AGENT_COLLECTION_TABLE = "lots_farmertoagentcollection";
    public static final String FARMER_TO_AGENT_KEY_FA_ID = "fa_id";
    public static final String FARMER_TO_AGENT_KEY_VC= "vendorcode";
    public static final String FARMER_TO_AGENT_KEY_LQ= "lotsquantity";
    public static final String FARMER_TO_AGENT_KEY_MR= "metrolacreading";
    public static final String FARMER_TO_AGENT_KEY_DRQ= "drq";
    public static final String FARMER_TO_AGENT_KEY_DPC= "dslprogrammecode";
    public static final String FARMER_TO_AGENT_KEY_EDT= "entrydatetime";
    public static final String FARMER_TO_AGENT_KEY_RE= "remarks";
    public static final String FARMER_TO_AGENT_KEY_STS= "status";

private static final String CREATE_FARMER_TO_AGENT_COLLECTION_TABLE = "CREATE TABLE "+FARMER_TO_AGENT_COLLECTION_TABLE+"  ("+FARMER_TO_AGENT_KEY_FA_ID+" integer primary key autoincrement,"+FARMER_TO_AGENT_KEY_VC+" varchar,"+FARMER_TO_AGENT_KEY_LQ+" varchar,"+FARMER_TO_AGENT_KEY_MR+" varchar,"+FARMER_TO_AGENT_KEY_DRQ+" varchar,"+FARMER_TO_AGENT_KEY_DPC+" varchar,"+FARMER_TO_AGENT_KEY_EDT+" varchar,"+FARMER_TO_AGENT_KEY_RE+" varchar,"+FARMER_TO_AGENT_KEY_STS+" varchar);";

@Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(CREATE_FARMER_TO_AGENT_COLLECTION_TABLE);
}
}

or please give me working sqlite insert full code.it's more helpful to me.

Harish
  • 576
  • 1
  • 8
  • 21
Yasi
  • 23
  • 6
  • 1
    I you've recently changed the schema (the `CREATE TABLE`), uninstall and reinstall your app to recreate the database. – laalto Sep 10 '15 at 04:53
  • you can traverse on your (DDMS) file explorer /data/data/your.app.package/databases/yourdatabase remove it – arun Sep 10 '15 at 04:58
  • @arun "data" on DDMS not extract.so how can i check is it inserted or not? – Yasi Sep 10 '15 at 05:01
  • did you have SQLLite Browser – arun Sep 10 '15 at 05:05
  • you don't then download here http://sqlitebrowser.org/ – arun Sep 10 '15 at 05:05
  • you meant data is not expand? – arun Sep 10 '15 at 05:06
  • i have data folder on DDMS.but i can't expand like (/data/data/your.app.package/databases/yourdatabase).i doble cllick on it.but it's not extract – Yasi Sep 10 '15 at 05:10
  • its a device or a emulator. – arun Sep 10 '15 at 05:11
  • but log cat says "09-10 10:36:53.635: D/testing(11377): lots db path /data/data/com.example.test_sqlite/databases/NEW.db 09-10 10:36:53.635: D/testing(11377): lots db exist true " – Yasi Sep 10 '15 at 05:12
  • i'm using Sony Xperia for testing. how can i view database on sqlite browser? – Yasi Sep 10 '15 at 05:12
  • oh! If you wanna use this Device must be rooted. you can just clear the data on your device settings/your app/clear data. – arun Sep 10 '15 at 05:14
  • how can i view database on sqlite browser? – Yasi Sep 10 '15 at 05:21
  • you need to copy the database to your sdcard ref http://stackoverflow.com/questions/17883447/how-to-check-database-on-not-rooted-android-device?answertab=votes#tab-top and then you can use sqllite browser – arun Sep 10 '15 at 05:24
  • kindly refer my this Answer : http://stackoverflow.com/questions/31398850/cant-create-table-with-4-column-sqlite-android-studio/31399056#31399056 – MDroid Sep 10 '15 at 07:26

0 Answers0