Note that there are some explanatory texts on larger screens.

plurals
  1. POData not storing in Sqlite table
    text
    copied!<p>Can anyone take a look at my code here. I am trying to store some data from my main layout using onClick method. here goes the codes for my onClick method</p> <pre><code>private OnClickListener bNextListener = new OnClickListener() { public void onClick(View v) { name=txt1.getText().toString(); email=txt2.getText().toString(); date = txtv.getText().toString(); gender = b4.getText().toString(); dbHelper.addProfiles(name, date, email, gender); }; </code></pre> <p>Whenever I run my application it crashes because it fails to store the data on the addProfiles method in my dbHelper class. Here goes my dbHelper class public class SqlHelper extends SQLiteOpenHelper { public static final String DATABASE_PATH = "/data/data/com.and.profile/databases/"; public static final String DATABASE_NAME = "profiledatabase.db"; private static final int DATABASE_VERSION = 2;</p> <pre><code>public static final String PROFILES_TABLE = "profiles"; public static final String COLUMN_PROFILE_ID = "profile_id"; public static final String COLUMN_NAME = "name"; public static final String COLUMN_BIRTHDAY = "birthday"; public static final String COLUMN_EMAIL = "email"; public static final String COLUMN_GENDER = "gender"; public static final String INTERESTS_TABLE = "interests"; public static final String COLUMN_ID = "_id"; public static final String COLUMN_TITLE = "title"; public static final String COLUMN_SELECTED = "selected"; private static final String CREATE_TABLE_1 = " create table " + PROFILES_TABLE + " (" + COLUMN_PROFILE_ID + " integer primary key autoincrement," + COLUMN_NAME + " name text not null, "+ COLUMN_BIRTHDAY +" birthday date not null, " + COLUMN_EMAIL + "email text not null, " + COLUMN_GENDER +" gender text not null);"; private static final String CREATE_TABLE_2 = " create table " + INTERESTS_TABLE + " (" + COLUMN_ID + " integer primary key autoincrement," + COLUMN_TITLE + " name text not null, "+ COLUMN_SELECTED +" selected integer);"; public SQLiteDatabase dbSqlite; //private boolean dbExist = true; private final Context myContext; public SqlHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); this.myContext = context; } @Override public void onCreate(SQLiteDatabase db) { // check if exists and copy database from resource //createDB(); db.execSQL(CREATE_TABLE_1); db.execSQL(CREATE_TABLE_2); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w("SqlHelper", "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data"); onCreate(db); } public void createDataBase() throws IOException{ boolean dbExist = CheckDataBase(); if(dbExist){ //do nothing - database already exist }else{ //By calling this method and empty database will be created into the default system path //of your application so we are gonna be able to overwrite that database with our database. this.getWritableDatabase(); copyDBFromResource(); } } boolean CheckDataBase() { SQLiteDatabase checkDB = null; try { String databasePath = DATABASE_PATH + DATABASE_NAME; checkDB = SQLiteDatabase.openDatabase(databasePath, null, SQLiteDatabase.OPEN_READWRITE); checkDB.setLocale(Locale.getDefault()); checkDB.setLockingEnabled(true); checkDB.setVersion(1); } catch (SQLiteException e) { Log.e("SqlHelper", "database not found"); } if (checkDB != null) { checkDB.close(); } return checkDB != null ? true : false; } private void copyDBFromResource() { InputStream inputStream = null; OutputStream outStream = null; String dbFilePath = DATABASE_PATH + DATABASE_NAME; try { inputStream = myContext.getAssets().open(DATABASE_NAME); outStream = new FileOutputStream(dbFilePath); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) &gt; 0) { outStream.write(buffer, 0, length); } outStream.flush(); outStream.close(); inputStream.close(); } catch (IOException e) { throw new Error("Problem copying database from resource file."); } } public void openDataBase() throws SQLException { String myPath = DATABASE_PATH + DATABASE_NAME; dbSqlite = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); } @Override public synchronized void close() { if (dbSqlite != null) dbSqlite.close(); super.close(); } public void addProfiles( String name, String date, String email, String gender) { System.out.println("addProfiles"); System.out.println("entre2"); dbSqlite.execSQL("INSERT INTO " + PROFILES_TABLE + " ('name','date','email','gender') values ('" + COLUMN_NAME + "', '" + COLUMN_BIRTHDAY + "', '" + COLUMN_EMAIL + "', '" + COLUMN_GENDER + "')"); } </code></pre> <p>} </p> <p>I will be very glad if some rapid helps provided by the experts. By the way I am using my database file inside assets folder if needed. </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