java - Made new SQLitedatabase Table does not exist -
my old sqlitedatabase wasn't working way wanted figured delete old dbadapter folder , dbhelper folder , made new database ground 2 new tables,new column names , new database_name after linked app new strings , cursors i'm getting nullpoint crash , i'm getting these errors in logcat.
update: i've changed oncreate parameter db , solved invalid tables error have table "players"(myfirsttable) not exist error.
update:new logcat
04-19 19:56:02.781: e/androidruntime(9179): fatal exception: main 04-19 19:56:02.781: e/androidruntime(9179): android.database.sqlite.sqliteexception: no such table: players (code 1): , while compiling: select _id, username players 04-19 19:56:02.781: e/androidruntime(9179): @ android.database.sqlite.sqliteconnection.nativepreparestatement(native method) 04-19 19:56:02.781: e/androidruntime(9179): @ android.database.sqlite.sqliteconnection.acquirepreparedstatement(sqliteconnection.java:882) 04-19 19:56:02.781: e/androidruntime(9179): @ android.database.sqlite.sqliteconnection.prepare(sqliteconnection.java:493) 04-19 19:56:02.781: e/androidruntime(9179): @ android.database.sqlite.sqlitesession.prepare(sqlitesession.java:588) 04-19 19:56:02.781: e/androidruntime(9179): @ android.database.sqlite.sqliteprogram.<init>(sqliteprogram.java:58) 04-19 19:56:02.781: e/androidruntime(9179): @ android.database.sqlite.sqlitequery.<init>(sqlitequery.java:37) 04-19 19:56:02.781: e/androidruntime(9179): @ android.database.sqlite.sqlitedirectcursordriver.query(sqlitedirectcursordriver.java:44) 04-19 19:56:02.781: e/androidruntime(9179): @ android.database.sqlite.sqlitedatabase.rawquerywithfactory(sqlitedatabase.java:1314) 04-19 19:56:02.781: e/androidruntime(9179): @ android.database.sqlite.sqlitedatabase.querywithfactory(sqlitedatabase.java:1161) 04-19 19:56:02.781: e/androidruntime(9179): @ android.database.sqlite.sqlitedatabase.query(sqlitedatabase.java:1032) 04-19 19:56:02.781: e/androidruntime(9179): @ android.database.sqlite.sqlitedatabase.query(sqlitedatabase.java:1200) 04-19 19:56:02.781: e/androidruntime(9179): @ com.fullfrontalgames.numberfighter.dbadapter.getusername(dbadapter.java:143) 04-19 19:56:02.781: e/androidruntime(9179): @ com.fullfrontalgames.numberfighter.findfriends$1.onclick(findfriends.java:39) 04-19 19:56:02.781: e/androidruntime(9179): @ android.view.view.performclick(view.java:4106) 04-19 19:56:02.781: e/androidruntime(9179): @ android.view.view$performclick.run(view.java:17052) 04-19 19:56:02.781: e/androidruntime(9179): @ android.os.handler.handlecallback(handler.java:615) 04-19 19:56:02.781: e/androidruntime(9179): @ android.os.handler.dispatchmessage(handler.java:92) 04-19 19:56:02.781: e/androidruntime(9179): @ android.os.looper.loop(looper.java:137) 04-19 19:56:02.781: e/androidruntime(9179): @ android.app.activitythread.main(activitythread.java:5059) 04-19 19:56:02.781: e/androidruntime(9179): @ java.lang.reflect.method.invokenative(native method) 04-19 19:56:02.781: e/androidruntime(9179): @ java.lang.reflect.method.invoke(method.java:511) 04-19 19:56:02.781: e/androidruntime(9179): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:792) 04-19 19:56:02.781: e/androidruntime(9179): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:555) 04-19 19:56:02.781: e/androidruntime(9179): @ dalvik.system.nativestart.main(native method)
dbadapter class
package com.fullfrontalgames.numberfighter; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteexception; import android.database.sqlite.sqliteopenhelper; import android.util.log; public class dbadapter { static final string key_rowid = "_id"; static final string key_username = "username"; static final string key_password = "password"; static final string key_email = "email"; static final string key_numberinput = "numberinput"; static final string key_score = "score"; static final string key_friends = "friends"; static final string tag = "dbadapter"; static final string database_name = "nfdatabase"; static final string database_table1 = "players"; static final string database_table2 = "friendslist"; static final int database_version = 1; static final string database_create_table1 = "create table players (_id integer primary key autoincrement," + "username text not null,password text not null,email text not null,numberinput text not null,,score text not null;"; static final string datbase_create_table2 = "create table friendslist (_id integer primary key autoincrement," + "friends text not null,username text not null,numberinput text not null,score text not null;"; final context context; databasehelper dbhelper; static sqlitedatabase db; public dbadapter(context ctx) { this.context = ctx; dbhelper = new databasehelper(context); } private static class databasehelper extends sqliteopenhelper { public databasehelper(context context) { // todo auto-generated constructor stub super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { // todo auto-generated method stub try { db.execsql(database_create_table1); db.execsql(database_create_table2); } catch (sqlexception e) { e.printstacktrace(); } } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub log.w(tag, "upgrading database version " + oldversion + " " + newversion + ", destroy old data"); db.execsql("drop table if exists players,friendslist"); oncreate(db); } } public dbadapter open() throws sqliteexception { db = dbhelper.getwritabledatabase(); return this; } public void close() { dbhelper.close(); } // players table crud public void insertplayer(string username, string password, string email) { contentvalues initialvalues = new contentvalues(); initialvalues.put("username", username); initialvalues.put("password", password); initialvalues.put("email", email); } public boolean deleteplayer(string username) { return db.delete("players", key_rowid + " = " + "_id", null) > 0; } public cursor getallplayers() { return db.query(null, new string[] { "_id", "username", "password", "email" }, null, null, null, null, null, null); } public string getdata() { string[] columns = new string[] { "_id", "username", "password" }; cursor mcursor = db.query("players", columns, null, null, null, null, null); string result = ""; int irow = mcursor.getcolumnindex(key_rowid); int iname = mcursor.getcolumnindex(key_username); (mcursor.movetofirst(); !mcursor.isafterlast(); mcursor.movetonext()) { result = mcursor.getstring(irow) + " " + mcursor.getstring(iname) + "/n"; } return result; } public cursor getplayer(long rowid) throws sqlexception { cursor mcursor = db.query(true, "players", new string[] { "_id", "username" }, key_rowid + " = " + "_id", null, null, null, null, null); if (mcursor != null) { mcursor.movetofirst(); } return mcursor; } public string getusername(string username) { // todo auto-generated method stub string[] columns = new string[] { "_id", "username" }; cursor mcursor = db.query("players", columns, null, null, null, null, null); string result = ""; int irow = mcursor.getcolumnindex(key_rowid); int iname = mcursor.getcolumnindex(key_username); (mcursor.movetofirst(); !mcursor.isafterlast(); mcursor.movetonext()) { result = mcursor.getstring(irow) + " " + mcursor.getstring(iname) + "/n"; } return result; } public string getsinlgeentry(string username) { cursor cursor = db.query("players", null, " username=?", new string[] { username }, null, null, null); if (cursor.getcount() < 1) // username not exist { cursor.close(); return "not exist"; } cursor.movetofirst(); string password = cursor.getstring(cursor.getcolumnindex("password")); cursor.close(); return password; } public boolean updateplayer(long _id, string username, string password, string email) { contentvalues args = new contentvalues(); args.put("username", username); args.put("password", password); args.put("email", email); return db.update("players", args, key_rowid + " = " + _id, null) > 0; } // friends table crud public void insertfriend(string friend) { contentvalues initialvalues = new contentvalues(); initialvalues.put("friends", friend); db.insert("friendslist", null, null); } public boolean deletefriend(string deletedfriend) { return db.delete("friendslist", key_rowid + " = " + "_id", null) > 0; } public cursor getallfriends() { return db.query(null, new string[] { "_id", "friends" }, null, null, null, null, null, null); } public cursor getfriend(long rowid) throws sqlexception { cursor mcursor = db.query(true, "friendslist", new string[] { "_id", "friends" }, key_rowid + " = " + "_id", null, null, null, null, null); if (mcursor != null) { mcursor.movetofirst(); } return mcursor; } public boolean updatefriend(long _id, string friends) { contentvalues args = new contentvalues(); args.put("friends", friends); return db.update("friendslist", args, key_rowid + " = " + _id, null) > 0; } }
here, have not named method argument db
other methods, guess shadowing static variable dbadapter.db
.
public void oncreate(sqlitedatabase arg0) { // todo auto-generated method stub try { db.execsql(database_create_table1); db.execsql(datbase_create_table2); } catch (sqlexception e) { e.printstacktrace(); } }
try changing arg0
db
.
Comments
Post a Comment