• Android SQLite. Need help, database file isn't creating at all.
    8 replies, posted
Hey, so am making android app which requires SQLite to be used, and I am using it for first time. So I made this file: [code]package com.example.droid; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class SqDb extends SQLiteOpenHelper{ private static final String NAME = SqDb.class.getSimpleName(); public static final String dName = "ExpenseR.db"; public static final int dVer = 2; public static final String table = "Expenses"; public SqDb(Context context) { super(context, dName, null, dVer); } @Override public void onCreate(SQLiteDatabase db) { try{ db.execSQL("CREATE TABLE " +table+ "(_id INT(5) PRIMARY KEY AUTOINCREMENT, _typ VARCHAR(20), _desc VARCHAR(255), _day INT(2),_mon INT(2),_cat VARCHAR(25),_val REAL,_time TIMESTAMP)"); } catch(Exception e){ e.printStackTrace(); } } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS "+table); onCreate(db); } } [/code] When I launch app, it doesn't create database (Checked through root explorer on phone), or print out any errors which is really pissing me off it neither works neither is broken,in other words acting like a cunt. Any solution why? Also how would I Read/Write (run queries) from other classes? I've been stuck with this for the past week. P.S - what's with crappy tutorials online when you search for simple tutorial, they seem to be showing you how to create whole app rather than just database bit. Thanks.
Well for starters are you making a database object somewhere in the app?
[QUOTE=FlashStock;39876375]Well for starters are you making a database object somewhere in the app?[/QUOTE] The way it must work is: When user launches app for first time (done using shared prefs), if he clicks register it brings to different activity where he fills in form then database is created. Or if he clicks skip registration: it just creates database. Then it's never created again. [editline]11th March 2013[/editline] I am stuck on this shit for like a week now :rolleyes:
I don't know if this will help but you can take a look at my school project I did a few months back, we used SQLite and Android so it might be useful.. [url]https://github.com/flashstock/QuizNavigator/tree/master/src/com/grupp4/quiznavigator/database[/url]
[QUOTE=arleitiss;39876401]The way it must work is: When user launches app for first time (done using shared prefs), if he clicks register it brings to different activity where he fills in form then database is created. Or if he clicks skip registration: it just creates database. Then it's never created again. [editline]11th March 2013[/editline] I am stuck on this shit for like a week now :rolleyes:[/QUOTE] You didn't really answer his question. Is the DB object being created, is it properly initializing? Can you run other queries? Are you sure that CREATE TABLE query is being ran? Are you able to manually create a table in that db (ensuring that there isn't some sort of permissions or writing issues with the db) Just some basic stuff to verify.
Wait... does it need to run at least 1 query to create DB? I thought that code is enough to just create a db file on device? (which is not created)
Okay after 2 hours of reading and making sense of FlashStocks' codes as examples, I kind of understood few things. Gonna completely re-write db class and see what happens then. Thanks for now, I will post results/questions after I am done. [editline]12th March 2013[/editline] Here, I re-wrote files yet It still doesn't work. Except that now it shows some error and I ended up having another twice as long file. Anyway: SqDb.java [code] package com.example.droid; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class SqDb extends SQLiteOpenHelper{ public static final String ACCOUNT = "account"; public static final String ACCOUNT_ID = "account_id"; public static final String ACCOUNT_USERNAME = "username"; public static final String ACCOUNT_PASSWORD = "password"; public static final String ACCOUNT_NAME = "account_name"; public static final String ACCOUNT_CITY = "account_city"; public static final String ACCOUNT_COUNTRY = "account_country"; public static final String ACCOUNT_AGE = "account_age"; public static final String GUEST = "guest"; public static final String GUEST_ID = "guest_id"; public static final String GUEST_NAME = "guest_name"; public static final String TRAN = "tran"; public static final String TRAN_ID = "tran_id"; public static final String TRAN_TYPE = "tran_type"; public static final String TRAN_DESC = "tran_desc"; public static final String TRAN_TIME = "tran_time"; public static final String TRAN_DAY = "tran_day"; public static final String TRAN_MON = "tran_mon"; public static final String TRAN_YEAR = "tran_year"; public static final String TRAN_CAT = "tran_cat"; public static final String TRAN_VAL = "tran_val"; public static final String CUR_MON = "cur_mon"; private static SqDb sqHelper; private static final String DATABASE_NAME = "expenser_data.db"; private static final int DATABASE_VERSION = 2; private static final String DATABASE_CREATE_USER = "CREATE TABLE " + ACCOUNT + "(" + ACCOUNT_ID + "INTEGER PRIMARY KEY AUTOINCREMENT, " + ACCOUNT_USERNAME + "TEXT UNIQUE NOT NULL, " + ACCOUNT_PASSWORD + "TEXT NOT NULL, " + ACCOUNT_NAME + "TEXT NOT NULL, " + ACCOUNT_CITY + "TEXT NOT NULL, " + ACCOUNT_COUNTRY + "TEXT NOT NULL, " + ACCOUNT_AGE + "INTEGER NOT NULL);"; private static final String DATABASE_CREATE_GUEST = "CREATE TABLE " + GUEST + "(" + GUEST_ID + "INTEGER PRIMARY KEY AUTOINCREMENT, " + GUEST_NAME + "TEXT NOT NULL);"; private static final String DATABASE_CREATE_TRANSACTION = "CREATE TABLE " + TRAN + "(" + TRAN_ID + "INTEGER PRIMARY KEY AUTOINCREMENT, " + TRAN_TYPE + "TEXT NOT NULL, " + TRAN_DESC + "TEXT NOT NULL, " + TRAN_TIME + "TEXT NOT NULL, " + TRAN_DAY + "INTEGER NOT NULL, " + TRAN_MON + "INTEGER NOT NULL, " + TRAN_YEAR + "INTEGER NOT NULL, " + TRAN_CAT + "TEXT NOT NULL, " + TRAN_VAL + "REAL NOT NULL);"; private SqDb(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } public static SqDb getInstance(Context context) { if(sqHelper == null) { sqHelper = new SqDb(context.getApplicationContext()); } return sqHelper; } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(DATABASE_CREATE_USER); db.execSQL(DATABASE_CREATE_GUEST); db.execSQL(DATABASE_CREATE_TRANSACTION); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + ACCOUNT); db.execSQL("DROP TABLE IF EXISTS " + GUEST); db.execSQL("DROP TABLE IF EXISTS " + TRAN); onCreate(db); } } [/code] then: DbCon.java [code] import com.example.droid.SqDb; import android.accounts.Account; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.util.Log; public class DbCon { private static SQLiteDatabase database; private SqDb SqHelper; private String[] UserColumns = { SqDb.ACCOUNT_ID, SqDb.ACCOUNT_NAME, SqDb.ACCOUNT_USERNAME, SqDb.ACCOUNT_PASSWORD, SqDb.ACCOUNT_AGE, SqDb.ACCOUNT_CITY, SqDb.ACCOUNT_COUNTRY }; private String [] GuestColumns = { SqDb.GUEST_ID, SqDb.GUEST_NAME }; private String [] TransColumns = { SqDb.TRAN_ID, SqDb.TRAN_DESC, SqDb.TRAN_TYPE, SqDb.TRAN_TIME, SqDb.TRAN_DAY, SqDb.TRAN_MON, SqDb.TRAN_YEAR, SqDb.TRAN_CAT, SqDb.TRAN_VAL }; private static DbCon myDCon; private DbCon(Context context) { SqHelper = SqDb.getInstance(context); } public static DbCon getInstance(Context context) { if(myDCon == null) { myDCon = new DbCon(context.getApplicationContext()); } return myDCon; } public void open() throws SQLException { database = SqHelper.getWritableDatabase(); } public void close() { SqHelper.close(); } public void createUser(String username, String password, String name, String Age, String City, String Country) { ContentValues values = new ContentValues(); values.put(SqDb.ACCOUNT_USERNAME, username); values.put(SqDb.ACCOUNT_PASSWORD, password); values.put(SqDb.ACCOUNT_NAME, name); values.put(SqDb.ACCOUNT_AGE, Age); values.put(SqDb.ACCOUNT_CITY, City); values.put(SqDb.ACCOUNT_COUNTRY, Country); database.insert(SqDb.ACCOUNT, null, values); } public void createGuest(String name) { ContentValues values = new ContentValues(); values.put(SqDb.GUEST_NAME, name); database.insert(SqDb.GUEST, null, values); } } [/code] Then there is a class called Prompt.java, it's an activity in which after clicking a button it should an entry by using createGuest(); [code] public void Skip(View v) throws Exception{ SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor es = settings.edit(); es.putBoolean("BeenRun", true); es.commit(); Intent Skip = new Intent(Prompt.this, Goals_act.class); Prompt.this.startActivity(Skip); dbs.createGuest("Stick Man"); } [/code] Full prompt,java: [code] package com.example.droid; import android.os.Bundle; import android.app.Activity; import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.view.Menu; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Spinner; public class Prompt extends Activity { public static final String PREFS_NAME = "Set"; DbCon dbs; public void Register(View v) throws Exception{ SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor es = settings.edit(); es.putBoolean("BeenRun", true); es.commit(); Intent Reg = new Intent(Prompt.this, Current_Month_act.class); Prompt.this.startActivity(Reg); } public void Skip(View v) throws Exception{ SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor es = settings.edit(); es.putBoolean("BeenRun", true); es.commit(); Intent Skip = new Intent(Prompt.this, Goals_act.class); Prompt.this.startActivity(Skip); dbs.createGuest("Stick Man"); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_prompt); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_prompt, menu); return true; } } [/code] Please help... I don't get it... Error it gives me: 03-12 01:44:54.149: D/AndroidRuntime(25257): Shutting down VM 03-12 01:44:54.149: W/dalvikvm(25257): threadid=1: thread exiting with uncaught exception (group=0x40018578) 03-12 01:44:54.159: E/AndroidRuntime(25257): FATAL EXCEPTION: main 03-12 01:44:54.159: E/AndroidRuntime(25257): java.lang.IllegalStateException: Could not execute method of the activity 03-12 01:44:54.159: E/AndroidRuntime(25257): at android.view.View$1.onClick(View.java:2144) 03-12 01:44:54.159: E/AndroidRuntime(25257): at android.view.View.performClick(View.java:2485) 03-12 01:44:54.159: E/AndroidRuntime(25257): at android.view.View$PerformClick.run(View.java:9080) 03-12 01:44:54.159: E/AndroidRuntime(25257): at android.os.Handler.handleCallback(Handler.java:587) 03-12 01:44:54.159: E/AndroidRuntime(25257): at android.os.Handler.dispatchMessage(Handler.java:92) 03-12 01:44:54.159: E/AndroidRuntime(25257): at android.os.Looper.loop(Looper.java:130) 03-12 01:44:54.159: E/AndroidRuntime(25257): at android.app.ActivityThread.main(ActivityThread.java:3687) 03-12 01:44:54.159: E/AndroidRuntime(25257): at java.lang.reflect.Method.invokeNative(Native Method) 03-12 01:44:54.159: E/AndroidRuntime(25257): at java.lang.reflect.Method.invoke(Method.java:507) 03-12 01:44:54.159: E/AndroidRuntime(25257): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 03-12 01:44:54.159: E/AndroidRuntime(25257): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 03-12 01:44:54.159: E/AndroidRuntime(25257): at dalvik.system.NativeStart.main(Native Method) 03-12 01:44:54.159: E/AndroidRuntime(25257): Caused by: java.lang.reflect.InvocationTargetException 03-12 01:44:54.159: E/AndroidRuntime(25257): at java.lang.reflect.Method.invokeNative(Native Method) 03-12 01:44:54.159: E/AndroidRuntime(25257): at java.lang.reflect.Method.invoke(Method.java:507) 03-12 01:44:54.159: E/AndroidRuntime(25257): at android.view.View$1.onClick(View.java:2139) 03-12 01:44:54.159: E/AndroidRuntime(25257): ... 11 more 03-12 01:44:54.159: E/AndroidRuntime(25257): Caused by: java.lang.NullPointerException 03-12 01:44:54.159: E/AndroidRuntime(25257): at com.example.droid.Prompt.Skip(Prompt.java:36) 03-12 01:44:54.159: E/AndroidRuntime(25257): ... 14 more
you're getting a null pointer exception on line 36 of prompt, which is [cpp] dbs.createGuest("Stick Man"); [/cpp] and from what I can see you never instantiate dbs. Also, you might find it easier to wrap troublesome code with try and catch [cpp] try{ //troublesome code }catch(Exception e){ Log.d("SQL", "Could not execute troublesome code, exception: " + e); } [/cpp] [editline]12th March 2013[/editline] Oh yeah and, I don't know if this is relevant, but some tasks like using the network will cause a fatal exception if you try and use them on the main thread of the app, so you have to use another thread.
I wrote: DbCon dbs = new DbCon(); but it says constructor is un-defined. Then it suggests to add context, I did but it still didn't work. I am noob at this but if I learn it I will know it [editline]12th March 2013[/editline] Nevermind, got it working. It successfully creates file and adds records. Just had to add few lines: DbCon dbs = new DbCon(this); dbs.open();
Sorry, you need to Log In to post a reply to this thread.