Note that there are some explanatory texts on larger screens.

plurals
  1. POSqlite database can not be create
    text
    copied!<p>I made simple classes for database, but when I test it on real device, application shut down!</p> <pre><code>public class MySQLiteHelper extends SQLiteOpenHelper { public static final String TABLE_NAME = "caffe"; public static final String COLUMN_ID = "_id"; public static final String COLUMN_ARTIKAL = "artikal"; public static final String COLUMN_PRICE = "price"; public static final String COLUMN_AMOUNT = "amount"; //public static final String COLUMN_TIME = "date&amp;time"; private static final String DATABASE_NAME = "caffe.db"; private static final int DATABASE_VERSION = 1; private static final String DATABASE_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);"; public MySQLiteHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase database) { database.execSQL(DATABASE_CREATE); } @Override public void onUpgrade(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); } </code></pre> <p>class DataAccessObject:</p> <pre><code>public class BD_DAO { private SQLiteDatabase database; private MySQLiteHelper dbHelper; private String[] allColumns = {MySQLiteHelper.COLUMN_ARTIKAL,MySQLiteHelper.COLUMN_PRICE, MySQLiteHelper.COLUMN_AMOUNT}; public BD_DAO(Context context) { dbHelper = new MySQLiteHelper(context); } public void open() throws SQLException { database = dbHelper.getWritableDatabase(); } public void close() { dbHelper.close(); } public void createItem(Item item) { ContentValues values = new ContentValues(); values.put(MySQLiteHelper.COLUMN_ARTIKAL, item.getArtikal().toString()); values.put(MySQLiteHelper.COLUMN_PRICE, Double.toString(item.getPrice()).toString()); values.put(MySQLiteHelper.COLUMN_AMOUNT, Integer.toString(item.getAmount()).toString()); database.insert(MySQLiteHelper.TABLE_NAME, null,values); } public List&lt;Item&gt; getAllItems() { List&lt;Item&gt; items = new ArrayList&lt;Item&gt;(); Cursor cursor = database.query(MySQLiteHelper.TABLE_NAME, allColumns, null, null, null, null, null); cursor.moveToFirst(); while (!cursor.isAfterLast()) { Item cursorItem = cursorToItem(cursor); items.add(cursorItem); cursor.moveToNext(); } cursor.close(); return items; } private Item cursorToItem(Cursor cursor) { Item item = new Item(); item.setArtikal(cursor.getString(1)); item.setPrice(cursor.getDouble(2)); item.setAmount(cursor.getInt(3)); return item; } } </code></pre> <p>And main class:</p> <pre><code>......listview, some buttons...... Button DB=(Button)findViewById(R.id.DB); DB.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { databasesource.open(); /*for (Item s:tableList) { databasesource.createItem(s);}*/ databasesource.close(); adapter.clear(); } }); </code></pre> <p>Application works fine until I press button, and then just shout down. First I tried to send some data in db, application shut down, then I tried just to create db, but shut down again! Should I make some changes is android manifest or some other .xml?</p> <p>Thanks in advance!!</p> <p>How to put logcat?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload