Android: Passing data from a table to another -
this scenario of system: 1. register user user go registeruser database (made in dbadapter) 2. if add user clicked, show listview of registered user (i trying pass names dbadapter table{from dbadapter2}
error happens when open activity.
currentusers.java
package com.example.istronggyminstructor; import java.util.arraylist; import android.os.bundle; import android.app.activity; import android.content.intent; import android.database.cursor; import android.view.gravity; import android.view.layoutinflater; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.view.viewgroup.layoutparams; import android.view.windowmanager; import android.widget.arrayadapter; import android.widget.button; import android.widget.edittext; import android.widget.framelayout; import android.widget.listview; import android.widget.popupwindow; import android.widget.textview; import android.widget.toast; public class currentusers extends activity { private button register; private button adduser; edittext getusertext, getpass, getweight, textdisp; dbadapter mydb; dbadapter2 mydb2; view popupview,popupview2; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_current_users); register = (button)findviewbyid(r.id.regbut); adduser = (button)findviewbyid(r.id.addbut); register.setonclicklistener(new onclicklistener(){ @override public void onclick(view arg0) { layoutinflater inflator=(layoutinflater)getbasecontext().getsystemservice(layout_inflater_service); popupview=inflator.inflate(r.layout.popup, null); final popupwindow popupwindow = new popupwindow(popupview, layoutparams.wrap_content,layoutparams.wrap_content); popupwindow.showatlocation(popupview, gravity.center, 0, 0); popupwindow.setfocusable(true); popupwindow.update(); button dismissbtn = (button)popupview.findviewbyid(r.id.close); dismissbtn.setonclicklistener(new onclicklistener(){ @override public void onclick(view arg0) { popupwindow.dismiss(); } }); popupwindow.showasdropdown(register, 50, -30); } }); opendb(); //thread.setdefaultuncaughtexceptionhandler(new forceclose(this)); listview lv = (listview) findviewbyid(r.id.listme); opendb2(); arraylist<string> data = mydb2.gedata(); closedb2(); lv.setadapter(new arrayadapter<string>(this, android.r.layout.simple_list_item_1, data)); } public void opendb(){ mydb = new dbadapter(this); mydb.open(); } public void opendb2(){ mydb2 = new dbadapter2(this); mydb2.open(); } private void closedb() { mydb.close(); } public void closedb2(){ mydb2.close(); } private void displaytext(string message) { opendb(); //textview textview = (textview) findviewbyid(r.id.); //textview.settext(message); } public void registeruser(view v) { opendb(); edittext username = (edittext)popupview.findviewbyid(r.id.usertext); edittext password = (edittext)popupview.findviewbyid(r.id.passwordtext); edittext weight = (edittext)popupview.findviewbyid(r.id.weight); string getusername = username.gettext().tostring(); string getpassword = password.gettext().tostring(); int getweight = integer.parseint(weight.gettext().tostring()); long newid = mydb.insertrow(getusername, getpassword, getweight); toast.maketext(getapplicationcontext(), "registering...", toast.length_short).show(); //cursor cursor = mydb.getrow(newid); } public void onclick_clearall(view v) { opendb(); mydb.deleteall(); } public void onclick_displayrecords(view v) { opendb(); cursor cursor = mydb.getallrows(); displayrecordset(cursor); } private void displayrecordset(cursor cursor) { string message = ""; opendb(); if (cursor.movetofirst()) { { int id = cursor.getint(dbadapter.col_rowid); string name = cursor.getstring(dbadapter.col_name); string password = cursor.getstring(dbadapter.col_password); int weight = integer.parseint(cursor.getstring(dbadapter.col_weight)); message += "name=" + name + "weight " + weight +"\n"; } while(cursor.movetonext()); } cursor.close(); displaytext(message); } public void onclick_adduser(view v){ layoutinflater inflator=(layoutinflater)getbasecontext().getsystemservice(layout_inflater_service); popupview2=inflator.inflate(r.layout.popup2, null); final popupwindow popupwindow = new popupwindow(popupview2, layoutparams.wrap_content,layoutparams.wrap_content); popupwindow.showatlocation(popupview2, gravity.center, 0, -10); popupwindow.setfocusable(true); popupwindow.update(); button dismissbtn = (button)popupview2.findviewbyid(r.id.close2); dismissbtn.setonclicklistener(new onclicklistener(){ @override public void onclick(view arg0) { popupwindow.dismiss(); } }); popupwindow.showasdropdown(register, 50, -30); mydb2.getuser(); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.current_users, menu); return true; } } xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <framelayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.16" > <textview android:id="@+id/textview1" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingleft="220dp" android:text="@string/title_activity_current_users" android:textsize="25sp" android:textstyle="bold" /> <textview android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingtop="98dp" android:text="@string/name" /> <textview android:id="@+id/textview3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingleft="200dp" android:paddingtop="98dp" android:text="@string/timein" /> <textview android:id="@+id/textview4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingleft="400dp" android:paddingtop="98dp" android:text="@string/score" /> <textview android:id="@+id/textview5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingleft="540dp" android:paddingtop="98dp" android:text="@string/done" /> <button android:id="@+id/regbut" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/register" /> <button android:id="@+id/addbut" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="505dp" android:onclick="onclick_adduser" android:text="@string/adduser" /> <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="40dp" android:onclick="onclick_clearall" android:text="@string/cleardb" /> </framelayout> <scrollview android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" > <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <textview android:id="@+id/textdisp" android:layout_width="match_parent" android:layout_height="738dp" android:layout_margintop="30dp" /> </linearlayout> </scrollview> </linearlayout> dbadapter.java
package com.example.istronggyminstructor; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.util.log; public class dbadapter { private static final string tag = "dbadapter"; public static final string key_rowid = "_id"; public static final int col_rowid = 0; public static final string key_username = "name"; public static final string key_password = "password"; public static final string key_weight = "weight"; public static final int col_name = 1; public static final int col_password = 2; public static final int col_weight = 3; public static final string[] all_keys = new string[] {key_rowid, key_username, key_password, key_weight}; public static final string database_name = "userdb"; public static final string database_table = "maintable"; // track db version if new version of app changes format. public static final int database_version = 2; private static final string database_create_sql = "create table " + database_table + " (" + key_rowid + " integer primary key autoincrement, " + key_username + " text not null, " + key_password + " text not null, " + key_weight + " integer not null" + ");"; private final context context; private databasehelper mydbhelper; private sqlitedatabase db; public dbadapter(context ctx) { this.context = ctx; mydbhelper = new databasehelper(context); } public dbadapter open() { db = mydbhelper.getwritabledatabase(); return this; } public void close() { mydbhelper.close(); } // add new set of values database. public long insertrow(string name, string password, int weight) { contentvalues initialvalues = new contentvalues(); initialvalues.put(key_username, name); initialvalues.put(key_password, password); initialvalues.put(key_weight, weight); // insert database. return db.insert(database_table, null, initialvalues); } public boolean deleterow(long rowid) { string = key_rowid + "=" + rowid; return db.delete(database_table, where, null) != 0; } public void deleteall() { cursor c = getallrows(); long rowid = c.getcolumnindexorthrow(key_rowid); if (c.movetofirst()) { { deleterow(c.getlong((int) rowid)); } while (c.movetonext()); } c.close(); } public cursor getallrows() { string = null; cursor c = db.query(true, database_table, all_keys, where, null, null, null, null, null); if (c != null) { c.movetofirst(); } return c; } public cursor getrow(long rowid) { string = key_rowid + "=" + rowid; cursor c = db.query(true, database_table, all_keys, where, null, null, null, null, null); if (c != null) { c.movetofirst(); } return c; } public boolean updaterow(long rowid, string name, string password,int weight) { string = key_rowid + "=" + rowid; contentvalues newvalues = new contentvalues(); newvalues.put(key_username, name); newvalues.put(key_password, password); newvalues.put(key_weight, weight); // insert database. return db.update(database_table, newvalues, where, null) != 0; } ///////////////////////////////////////////////////////////////////// // private helper classes: ///////////////////////////////////////////////////////////////////// /** * private class handles database creation , upgrading. * used handle low-level database access. */ private static class databasehelper extends sqliteopenhelper { databasehelper(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase _db) { _db.execsql(database_create_sql); } @override public void onupgrade(sqlitedatabase _db, int oldversion, int newversion) { log.w(tag, "upgrading application's database version " + oldversion + " " + newversion + ", destroy old data!"); // destroy old database: _db.execsql("drop table if exists " + database_table); // recreate new database: oncreate(_db); } } } dbadapter2.java
package com.example.istronggyminstructor; import java.util.arraylist; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.util.log; public class dbadapter2 { private static final string tag = "dbadapter2"; public static final string key_rowid = "_id"; public static final int col_rowid = 0; public static final string key_username = "name"; public static final int col_name = 1; public static final string[] all_keys = new string[] {key_rowid, key_username}; public static final string database_name = "currentuserdb"; public static final string database_table = "addusertable"; // track db version if new version of app changes format. public static final int database_version = 2; private static final string database_create_sql = "create table " + database_table + " (" + key_rowid + " integer primary key autoincrement, " + key_username + " text not null, " + ");"; private final context context; private databasehelper mydbhelper; private sqlitedatabase db; public dbadapter2(context ctx) { this.context = ctx; mydbhelper = new databasehelper(context); } public dbadapter2 open() { db = mydbhelper.getwritabledatabase(); return this; } public void close() { mydbhelper.close(); } // add new set of values database. public void getuser() { db.execsql("insert currentuserdb.addusertable select name userdb.maintable"); } public cursor getallrows() { string = null; cursor c = db.query(true, database_table, all_keys, where, null, null, null, null, null); if (c != null) { c.movetofirst(); } return c; } public cursor getrow(long rowid) { string = key_rowid + "=" + rowid; cursor c = db.query(true, database_table, all_keys, where, null, null, null, null, null); if (c != null) { c.movetofirst(); } return c; } public arraylist<string> gedata() { string[]columns=new string[]{ key_rowid,key_username}; cursor c =db.query(database_table, columns, null, null, null, null, null); arraylist<string> result = new arraylist<string>(); int iname=c.getcolumnindex(key_username); for(c.movetofirst(); !c.isafterlast();c.movetonext()){ result.add(c.getstring(iname)); } return result; } ///////////////////////////////////////////////////////////////////// // private helper classes: ///////////////////////////////////////////////////////////////////// /** * private class handles database creation , upgrading. * used handle low-level database access. */ private static class databasehelper extends sqliteopenhelper { databasehelper(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase _db) { _db.execsql(database_create_sql); } @override public void onupgrade(sqlitedatabase _db, int oldversion, int newversion) { log.w(tag, "upgrading application's database version " + oldversion + " " + newversion + ", destroy old data!"); // destroy old database: _db.execsql("drop table if exists " + database_table); // recreate new database: oncreate(_db); } } } logcat
08-19 02:50:23.374: e/androidruntime(14680): fatal exception: main 08-19 02:50:23.374: e/androidruntime(14680): java.lang.runtimeexception: unable start activity componentinfo{com.example.istronggyminstructor/com.example.istronggyminstructor.currentusers}: android.database.sqlite.sqliteexception: near ")": syntax error (code 1): , while compiling: create table addusertable (_id integer primary key autoincrement, name text not null, ); 08-19 02:50:23.374: e/androidruntime(14680): @ android.app.activitythread.performlaunchactivity(activitythread.java:2180) 08-19 02:50:23.374: e/androidruntime(14680): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 08-19 02:50:23.374: e/androidruntime(14680): @ android.app.activitythread.access$600(activitythread.java:141) 08-19 02:50:23.374: e/androidruntime(14680): @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 08-19 02:50:23.374: e/androidruntime(14680): @ android.os.handler.dispatchmessage(handler.java:99) 08-19 02:50:23.374: e/androidruntime(14680): @ android.os.looper.loop(looper.java:137) 08-19 02:50:23.374: e/androidruntime(14680): @ android.app.activitythread.main(activitythread.java:5041) 08-19 02:50:23.374: e/androidruntime(14680): @ java.lang.reflect.method.invokenative(native method) 08-19 02:50:23.374: e/androidruntime(14680): @ java.lang.reflect.method.invoke(method.java:511) 08-19 02:50:23.374: e/androidruntime(14680): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 08-19 02:50:23.374: e/androidruntime(14680): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 08-19 02:50:23.374: e/androidruntime(14680): @ dalvik.system.nativestart.main(native method) 08-19 02:50:23.374: e/androidruntime(14680): caused by: android.database.sqlite.sqliteexception: near ")": syntax error (code 1): , while compiling: create table addusertable (_id integer primary key autoincrement, name text not null, ); 08-19 02:50:23.374: e/androidruntime(14680): @ android.database.sqlite.sqliteconnection.nativepreparestatement(native method) 08-19 02:50:23.374: e/androidruntime(14680): @ android.database.sqlite.sqliteconnection.acquirepreparedstatement(sqliteconnection.java:882) 08-19 02:50:23.374: e/androidruntime(14680): @ android.database.sqlite.sqliteconnection.prepare(sqliteconnection.java:493) 08-19 02:50:23.374: e/androidruntime(14680): @ android.database.sqlite.sqlitesession.prepare(sqlitesession.java:588) 08-19 02:50:23.374: e/androidruntime(14680): @ android.database.sqlite.sqliteprogram.<init>(sqliteprogram.java:58) 08-19 02:50:23.374: e/androidruntime(14680): @ android.database.sqlite.sqlitestatement.<init>(sqlitestatement.java:31) 08-19 02:50:23.374: e/androidruntime(14680): @ android.database.sqlite.sqlitedatabase.executesql(sqlitedatabase.java:1663) 08-19 02:50:23.374: e/androidruntime(14680): @ android.database.sqlite.sqlitedatabase.execsql(sqlitedatabase.java:1594) 08-19 02:50:23.374: e/androidruntime(14680): @ com.example.istronggyminstructor.dbadapter2$databasehelper.oncreate(dbadapter2.java:116) 08-19 02:50:23.374: e/androidruntime(14680): @ android.database.sqlite.sqliteopenhelper.getdatabaselocked(sqliteopenhelper.java:252) 08-19 02:50:23.374: e/androidruntime(14680): @ android.database.sqlite.sqliteopenhelper.getwritabledatabase(sqliteopenhelper.java:164) 08-19 02:50:23.374: e/androidruntime(14680): @ com.example.istronggyminstructor.dbadapter2.open(dbadapter2.java:51) 08-19 02:50:23.374: e/androidruntime(14680): @ com.example.istronggyminstructor.currentusers.opendb2(currentusers.java:75) 08-19 02:50:23.374: e/androidruntime(14680): @ com.example.istronggyminstructor.currentusers.oncreate(currentusers.java:62) 08-19 02:50:23.374: e/androidruntime(14680): @ android.app.activity.performcreate(activity.java:5104) 08-19 02:50:23.374: e/androidruntime(14680): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) 08-19 02:50:23.374: e/androidruntime(14680): @ android.app.activitythread.performlaunchactivity(activitythread.java:2144) 08-19 02:50:23.374: e/androidruntime(14680): ... 11 more
you have syntax error while compiling create table addusertable ......
try removing comma after 'not null' on 'database_create_sql' in dbadapater2 class.
private static final string database_create_sql = "create table " + database_table + " (" + key_rowid + " integer primary key autoincrement, " + key_username + " text not null " + ");";
Comments
Post a Comment