0

I know that there are a lot of topics on this error but I tried a lot of solutions and still I am stuck whit this error. It´s my first time playing around with SQLite, and I don´t understand what is happening wrong there.and I can't add data from second Activity but frist Activity that work.

it my class

public static final String KEY_ID = "_ID";
public static final String COL_CODE_TREE = "code_tree";
public static final String COL_STATUS_HIGH = "updateStatus";

public static final String TABLE_NAME_Product = "Product";
public static final String COL_PRODUCT_ALL = "product_all";
public static final String COL_PRODUCT_BEST = "product_best";
public static final String COL_DATE_PRODUCT = "date_product";



public DatabaseCoconut(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}

public void onCreate(SQLiteDatabase db) {

    String query,queryProduct;
    query = "CREATE TABLE High ( _Id INTEGER PRIMARY KEY, code_tree VARCHAR, high_tree INTEGER, date_high DATE, updateStatus TEXT)";
    queryProduct = "CREATE TABLE Product ( _Id INTEGER PRIMARY KEY, code_tree VARCHAR, product_all INTEGER, product_best INTEGER , date_product DATE, updateStatus TEXT)";
    db.execSQL(query);
    db.execSQL(queryProduct);

}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
   // db.execSQL("DROP TABLE IF EXISTS "+ TABLE_NAME_HIGH);
    String query,queryProduct;
    query = "DROP TABLE IF EXISTS High";
    queryProduct = "DROP TABLE IF EXISTS Product";
    db.execSQL(query);
    db.execSQL(queryProduct);
    onCreate(db);
}

secondActivity

public class ProductActivity extends AppCompatActivity{

public static final String BARCODE_KEY = "BARCODE";
private Barcode barcodeResult;
private EditText result;

ListView listproductshow;
DatabaseCoconut mHelper;
SQLiteDatabase mDb2;
Cursor mCursor2;
DatabaseCoconut controller = new DatabaseCoconut(this);
ProgressDialog prgDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_product);
    Log.d("ProductActivity", "onCreate");

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    result = (EditText) findViewById(R.id.editCodeProduct);
    final EditText editall = (EditText)findViewById(R.id.editproductall);
    final EditText editbest = (EditText)findViewById(R.id.editproductbest);

    mHelper = new DatabaseCoconut(this);
    mDb2 = mHelper.getWritableDatabase();

    //Toast.makeText(getApplicationContext(), mHelper.getSyncStatus(), Toast.LENGTH_LONG).show();

    Button buttonAdd = (Button)findViewById(R.id.buttonAddproduct);
    buttonAdd.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            String codeproduct = result.getText().toString();
            String all = editall.getText().toString();
            String best = editbest.getText().toString();
            String no = "no";

            if(codeproduct.trim().length() != 0 && all.trim().length() != 0 && best.trim().length() != 0 ) {

                Cursor mCursor = mDb2.rawQuery("SELECT * FROM " + DatabaseCoconut.TABLE_NAME_Product
                        + " WHERE " + DatabaseCoconut.COL_CODE_TREE + "='" + codeproduct + "'"
                        + " AND " + DatabaseCoconut.COL_PRODUCT_ALL + "='" + all + "'"
                        + " AND " + DatabaseCoconut.COL_PRODUCT_BEST + "='" + best + "'"
                        + " AND " + DatabaseCoconut.COL_STATUS_HIGH + "='" + no + "'"
                        + " AND " + DatabaseCoconut.COL_DATE_PRODUCT + "='" + getDateTime() + "'", null);

                if(mCursor.getCount() == 0) {
                    mDb2.execSQL("INSERT INTO "+ DatabaseCoconut.TABLE_NAME_Product
                            +" (" + DatabaseCoconut.COL_CODE_TREE
                            + ", " + DatabaseCoconut.COL_PRODUCT_ALL
                            + ", " + DatabaseCoconut.COL_PRODUCT_BEST
                            + ", " + DatabaseCoconut.COL_STATUS_HIGH
                            + ", " + DatabaseCoconut.COL_DATE_PRODUCT + ") VALUES ('"
                            + codeproduct + "', '" + all + "', '" + best + "', '" + no + "', '" + getDateTime() + "');");

                    result.setText("");
                    editall.setText("");
                    editbest.setText("");

                    Toast.makeText(getApplicationContext()
                            ,"เพิ่มข้อมูลเรียบร้อยแล้ว", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext()
                            ,"คุณมีข้อมูลนี้อยู่แล้ว", Toast.LENGTH_SHORT).show();
                }

            } else {
                Toast.makeText(getApplicationContext()
                        ,"กรุณากรอกข้อมูลให้ครบทุกช่อง", Toast.LENGTH_SHORT).show();
            }

            mCursor2 = mDb2.rawQuery("SELECT * FROM "
                    + DatabaseCoconut.TABLE_NAME_Product, null);

            //listproductshow = (ListView)findViewById(R.id.listhigh);
            //listproductshow.setAdapter(updateListView());

        }
    });
      private String getDateTime() {
    SimpleDateFormat dateFormat = new SimpleDateFormat(
            "yyyy-MM-dd ", Locale.getDefault());
    Date date = new Date();
    return dateFormat.format(date);
}

My Logcat

ATAL EXCEPTION: main
                                                                       Process: com.nectec.ProjectCoconut, PID: 27718
                                                                       android.database.sqlite.SQLiteException: no such table: Product (Sqlite code 1): , while compiling: SELECT * FROM Product WHERE code_tree='hi' AND product_all='99' AND product_best='56' AND updateStatus='no' AND date_product='2017-04-08 ', (OS error - 2:No such file or directory)
                                                                           at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                           at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:897)
                                                                           at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:508)
                                                                           at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                           at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:63)
                                                                           at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
                                                                           at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
                                                                           at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1344)
                                                                           at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1278)
                                                                           at com.nectec.ProjectCoconut.ProductActivity$1.onClick(ProductActivity.java:90)
                                                                           at android.view.View.performClick(View.java:5264)
                                                                           at android.view.View$PerformClick.run(View.java:21297)
                                                                           at android.os.Handler.handleCallback(Handler.java:743)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:150)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5546)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
  • Your product table wasn't created. Make sure you didn't ignore an exception earlier. – Gabe Sechan Apr 07 '17 at 19:18
  • "CREATE TABLE Product ( _Id INTEGER PRIMARY KEY, code_tree VARCHAR, product_all INTEGER, product_best INTEGER , date_product DATE, updateStatus TEXT)"; in fuction onCreate(SQLiteDatabase db) – Jaturathep Srivarapong Apr 07 '17 at 19:24
  • You have code to create it, but it didn't create successfully or that code wasn't called. That's why the exception says "Table does not exist". – Gabe Sechan Apr 07 '17 at 19:26

1 Answers1

0

It is possible that you executed your code the first time without creating the "Product" table in your onCreate(SQLiteDatabase db) method. As you run the code with the same database version number in second time, it does not update it and tells you that the table does not exist. Simply manually uninstall the application or delete the data to force the rebuilding of the database with the same version DB_VERSION.

J-Jamet
  • 847
  • 8
  • 17