Sqlite Database Can Not Be Create
I made simple classes for database, but when I test it on real device, application shut down! public class MySQLiteHelper extends SQLiteOpenHelper { public static final String
Solution 1:
try this way.
DB.setOnClickListener(newView.OnClickListener(){
@OverridepublicvoidonClick(View v) {
BD_DAOdb=newBD_DAO(getApplicationContext());
//make your CRUD operation and then close database object
db.close();
}
});
Replay this code with your existing code.
privatestaticfinalStringDATABASE_CREATE="create table " +
TABLE_NAME + "(" + COLUMN_ID +
" integer primary key autoincrement," + COLUMN_ARTIKAL +
" text not null ," + COLUMN_PRICE + " real not null ," +
COLUMN_AMOUNT + " integer not null)";
if you find any trouble then put comment.
Solution 2:
I have made few minor changes with your code. You can use the same for reference.
Modify the below according to your requirements. I had a similar sample and i modified the same with your code. I am not sure if this is the best way. But you can use the below and modify the same according to your requirements.
In Your MainActivity to add
Button DB=(Button)findViewById(R.id.DB);
DB.setOnClickListener(newView.OnClickListener()
{
@OverridepublicvoidonClick(View v)
{
Doubleprice= Double.parseDouble(et2.getText().toString());
intamount= Integer.parseInt(et3.getText().toString());
MySQLiteHelperdb=newMySQLiteHelper (MainActivity.this);
db.createItem(newItem(et1.getText().toString(),price,amount));
db.close();
}
});
MySQLiteHelper
publicclassMySQLiteHelperextendsSQLiteOpenHelper {
publicstaticfinalStringTABLE_NAME="caffe";
// private SQLiteDatabase database;// private MySQLiteHelper dbHelper;private String[] allColumns = {MySQLiteHelper.COLUMN_ARTIKAL,MySQLiteHelper.COLUMN_PRICE, MySQLiteHelper.COLUMN_AMOUNT};
publicstaticfinalStringCOLUMN_ID="_id";
publicstaticfinalStringCOLUMN_ARTIKAL="artikal";
publicstaticfinalStringCOLUMN_PRICE="price";
publicstaticfinalStringCOLUMN_AMOUNT="amount";
// public static final String COLUMN_TIME = "date&time";
Context context;
privatestaticfinalStringDATABASE_NAME="caffe.db";
privatestaticfinalintDATABASE_VERSION=1;
// changed the below privatestaticfinalStringDATABASE_CREATE="create table " +
TABLE_NAME + "(" + COLUMN_ID +
" integer primary key autoincrement," + COLUMN_ARTIKAL +
" text not null," + COLUMN_PRICE + " real not null," +
COLUMN_AMOUNT + " integer not null)";
publicMySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
context= context;
}
@OverridepublicvoidonCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
@OverridepublicvoidonUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
publicvoidcreateItem(Item item) {
SQLiteDatabasedb=this.getWritableDatabase();
ContentValuesvalues=newContentValues();
values.put(MySQLiteHelper.COLUMN_ARTIKAL, item.getArticle().toString());
values.put(MySQLiteHelper.COLUMN_PRICE, Double.toString(item.getPrice()).toString());
values.put(MySQLiteHelper.COLUMN_AMOUNT, Integer.toString(item.getAmount()).toString());
//Toast.makeText(context, "Inserted", 1000).show();
db.insert(MySQLiteHelper.TABLE_NAME, null,values);
}
public List<Item> getAllItems() {
List<Item> items = newArrayList<Item>();
SQLiteDatabasedb=this.getWritableDatabase();
Cursorcursor= db.query(MySQLiteHelper.TABLE_NAME,
allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
ItemcursorItem= cursorToItem(cursor);
items.add(cursorItem);
cursor.moveToNext();
}
cursor.close();
return items;
}
private Item cursorToItem(Cursor cursor) {
Itemitem=newItem();
item.setArticle(cursor.getString(0));
item.setPrice(cursor.getDouble(1));
item.setAmount(cursor.getInt(2));
return item;
}
}
Item class
publicclassItem {
String article;
Double price;
int amount;
publicItem()
{
}
publicItem(String article,Double price, int amount)
{
this.article=article;
this.price=price;
this.amount= amount;
}
public String getArticle() {
return article;
}
publicvoidsetArticle(String article) {
this.article = article;
}
public Double getPrice() {
return price;
}
publicvoidsetPrice(Double price) {
this.price = price;
}
publicintgetAmount() {
return amount;
}
publicvoidsetAmount(int amount) {
this.amount = amount;
}
}
Snap shot
Display on show button click. The toast displays only article name.
MySQLiteHelperdb=newMySQLiteHelper (MainActivity.this);
List<Item> contacts = db.getAllItems();
StringBuildersb=newStringBuilder();
for (Item cn : contacts)
{
sb.append(cn.getArticle());
sb.append("\n");
}
Toast.makeText(MainActivity.this,sb, 1000).show();
Solution 3:
You forgot to put a comma replace this code of your
privatestaticfinalStringDATABASE_CREATE="create table " +
TABLE_NAME + "(" + COLUMN_ID +
" integer primary key autoincrement," + COLUMN_ARTIKAL +
" text not null" + COLUMN_PRICE + " real not null" +
COLUMN_AMOUNT + " integer not null);";
to
privatestaticfinalStringDATABASE_CREATE="create table " +
TABLE_NAME + "(" + COLUMN_ID +
" integer primary key autoincrement," + COLUMN_ARTIKAL +
" text not null," + COLUMN_PRICE + " real not null," +
COLUMN_AMOUNT + " integer not null)";
Post a Comment for "Sqlite Database Can Not Be Create"