Edit-Well I fixed the problem that I was facing as comma was the issue in the query, but now I am not getting the required output ie when I add a new info to the db it doesn't get displayed(printed) in the text view. Can you guys please check whether there's a bug in the printDatabase() function(although I am not getting errors in log-cat monitor).
package com.instinctcode.sqlitesample;
public class MainActivity extends AppCompatActivity {
EditText buckysInput;
TextView buckysText;
MyDBHandler dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buckysInput=(EditText)findViewById(R.id.buckysInput);
buckysText=(TextView)findViewById(R.id.buckysText);
dbHandler= new MyDBHandler(this,null,null,1);
printDatabase();
}
public void addButtonClicked(View view){
Products product=new Products(buckysInput.getText().toString());
dbHandler.addProduct(product);
printDatabase();
}
public void deleteButtonClicked(View view)
{
String inputText=buckysInput.getText().toString();
dbHandler.deleteProduct(inputText);
printDatabase();
}
public void printDatabase(View v){
String dbString =dbHandler.databaseToString();
buckysText.setText(dbString);
buckysInput.setText("");
}
}
public class Products {
private int _id;
private String _productname;
public Products(String productname) {
this._productname = productname;
}
public void set_id(int _id) {
this._id = _id;
}
public void set_productname(String _productname) {
this._productname =_productname;
}
public int get_id() {
return _id;
}
public String get_productname() {
return _productname;
}
}
public class MyDBHandler extends SQLiteOpenHelper {
private static final int DATAVASE_VERSION=2;
private static final String DATABASE_NAME="products.db";
public static final String TABLE_PRODUCTS="products";
public static final String COLUMN_ID ="_id";
public static final String COLUMN_PRODUCTNAME = "productname";
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query="CREATE TABLE " + TABLE_PRODUCTS + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT " +
COLUMN_PRODUCTNAME + " TEXT " +
");";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
onCreate(db);
}
public void addProduct(Products product){
ContentValues values=new ContentValues();
values.put(COLUMN_PRODUCTNAME, product.get_productname());
SQLiteDatabase db=getWritableDatabase();
db.insert(TABLE_PRODUCTS, null, values);
db.close();
}
public void deleteProduct(String productName)
{
SQLiteDatabase db = getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + "=\"" + productName + "\";");
}
public String databaseToString(){
String dbString ="";
SQLiteDatabase db= getWritableDatabase();
String query= "SELECT * FROM "+ TABLE_PRODUCTS+ "WHERE 1";
Cursor c=db.rawQuery(query,null);
c.moveToFirst();
while(!c.isAfterLast())
{
if(c.getString(c.getColumnIndex("productname"))!=null)
{
dbString +=c.getString(c.getColumnIndex("productname"));
dbString += "\n";
}
}
db.close();
return dbString;
}
}
Comma ,
is missing:
CREATE TABLE products(_id INTEGER PRIMARY KEY AUTOINCREMENT, productname TEXT )
^
Change your query with this..
@Override
public void onCreate(SQLiteDatabase db) {
String query="CREATE TABLE " + TABLE_PRODUCTS + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_PRODUCTNAME + " TEXT " +
")";
db.execSQL(query);
}
https://stackoverflow.com/a/41327975/5208657 well this worked for me. A comma was missing after the AUTO INCREMENT statement, and that caused the error. But still I haven't been able to successfully print the results after adding the product info to the sqlite db (although I am not getting any errors in the monitor).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.