我正在创建一个获取用户名密码并注册它们的活动我面临成员注册活动中的sqlite异常问题,任何人都可以指出我在下面的代码中做错了什么?发布了更新的LOGCAT
DBHelper活动
public class DBHelper扩展了SQLiteOpenHelper {
private static final String DATABASE_NAME = "membersdb";
private static final String DATABASE_TABLE = "members"; // this is your
// table name
private static final String KEY_ROW_ID = "_id";
private static final String KEY_USERNAME = "username";
private static final String KEY_PASSWORD = "password";
private static final String KEY_FULLNAME = "fullname";
private static final String KEY_EMAILS = "email";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "CREATE TABLE "
+ DATABASE_TABLE + " (" + KEY_ROW_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_USERNAME
+ " TEXT NOT NULL , " + KEY_PASSWORD + " TEXT NOT NULL , "
+ KEY_FULLNAME + " TEXT NOT NULL , " + KEY_EMAILS
+ " TEXT NOT NULL);";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
db.execSQL("DROP TABLE IF EXISTS members");
onCreate(db);
}
}
DBAdapter活动
public class DBAdapter {
private static final String DATABASE_TABLE = "members";
public static final String KEY_ROW_ID = "_id";
public static final String KEY_USERNAME = "username";
public static final String KEY_PASSWORD = "password";
public static final String KEY_FULLNAME = "fullname";
public static final String KEY_EMAILS = "emails";
SQLiteDatabase mDb;
Context mCtx;
DBHelper mDbHelper;
public DBAdapter(Context context) {
this.mCtx = context;
}
public DBAdapter open() throws SQLException {
mDbHelper = new DBHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
public long register(String user, String pw, String name, String email) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_USERNAME, user);
initialValues.put(KEY_PASSWORD, pw);
initialValues.put(KEY_FULLNAME, name);
initialValues.put(KEY_EMAILS, email);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
public boolean Login(String username, String password) throws SQLException {
Cursor mCursor = mDb.rawQuery("SELECT * FROM " + DATABASE_TABLE
+ " WHERE username=? AND password=?", new String[] { username,
password });
if (mCursor != null) {
if (mCursor.getCount() > 0) {
return true;
}
}
return false;
}
}
调用DBadapter的主要活动
public class MembersActivity extends Activity {
DBAdapter dbAdapter;
EditText txtUserName;
EditText txtPassword;
Button btnLogin;
Button btnRegister;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtUserName = (EditText) findViewById(R.id.et_user);
txtPassword = (EditText) findViewById(R.id.et_pw);
btnLogin = (Button) findViewById(R.id.btn_login);
btnRegister = (Button) findViewById(R.id.btn_reg);
dbAdapter = new DBAdapter(this);
dbAdapter.open();
btnLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(txtUserName.getWindowToken(), 0);
imm.hideSoftInputFromWindow(txtPassword.getWindowToken(), 0);
String username = txtUserName.getText().toString();
String password = txtPassword.getText().toString();
if (username.length() > 0 && password.length() > 0) {
try {
if (dbAdapter.Login(username, password)) {
Toast.makeText(MembersActivity.this,
"Successfully Logged In", Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(MembersActivity.this,
"Invalid username or password",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(MembersActivity.this,
"Some problem occurred", Toast.LENGTH_LONG)
.show();
}
} else {
Toast.makeText(MembersActivity.this,
"Username or Password is empty", Toast.LENGTH_LONG)
.show();
}
}
});
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent opencont = new Intent("com.ideals.ideal.MEMBERSREGISTER");
startActivity(opencont);
}
});
}
}
logcat的
09-29 21:46:07.577: I/Database(555): sqlite returned: error code = 1, msg = table members has no column named emails
09-29 21:46:07.587: E/Database(555): Error inserting username=hello emails=hello password=hello fullname=hello
09-29 21:46:07.587: E/Database(555): android.database.sqlite.SQLiteException: table members has no column named emails: , while compiling: INSERT INTO members(username, emails, password, fullname) VALUES(?, ?, ?, ?);
09-29 21:46:07.587: E/Database(555): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
09-29 21:46:07.587: E/Database(555): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
09-29 21:46:07.587: E/Database(555): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
09-29 21:46:07.587: E/Database(555): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
09-29 21:46:07.587: E/Database(555): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41)
09-29 21:46:07.587: E/Database(555): at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1149)
09-29 21:46:07.587: E/Database(555): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1569)
09-29 21:46:07.587: E/Database(555): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1426)
09-29 21:46:07.587: E/Database(555): at com.ideals.ideal.DBAdapter.register(DBAdapter.java:42)
09-29 21:46:07.587: E/Database(555): at com.ideals.ideal.MembersRegister$1.onClick(MembersRegister.java:50)
09-29 21:46:07.587: E/Database(555): at android.view.View.performClick(View.java:2485)
09-29 21:46:07.587: E/Database(555): at android.view.View$PerformClick.run(View.java:9080)
09-29 21:46:07.587: E/Database(555): at android.os.Handler.handleCallback(Handler.java:587)
09-29 21:46:07.587: E/Database(555): at android.os.Handler.dispatchMessage(Handler.java:92)
09-29 21:46:07.587: E/Database(555): at android.os.Looper.loop(Looper.java:123)
09-29 21:46:07.587: E/Database(555): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-29 21:46:07.587: E/Database(555): at java.lang.reflect.Method.invokeNative(Native Method)
09-29 21:46:07.587: E/Database(555): at java.lang.reflect.Method.invoke(Method.java:507)
09-29 21:46:07.587: E/Database(555): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-29 21:46:07.587: E/Database(555): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-29 21:46:07.587: E/Database(555): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
似乎你错过了表名,试试这样:
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "membersdb";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "CREATE TABLE members (_id integer primary key autoincrement, username text not null, password text not null);";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
db.execSQL("DROP TABLE IF EXISTS members");
onCreate(db);
}
}
答案 1 :(得分:1)
有这些
private static final String DATABASE_TABLE = "members"; // this is your table name
private static final String KEY_ROW_ID = "_id";
private static final String KEY_USERNAME = "username";
private static final String KEY_PASSWORD = "password";
DBHelper
中的
然后您的查询应该如下所示
private static final String DATABASE_CREATE = "CREATE TABLE " + DATABASE_TABLE + " ("
+ KEY_ROW_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_USERNAME + " TEXT NOT NULL , " +
KEY_PASSWORD + " TEXT NOT NULL );";
答案 2 :(得分:1)
您尚未提及表名。 logcat也专注于:
09-29 20:17:09.747: E/AndroidRuntime(372): Caused by: android.database.sqlite.SQLiteException: near "(": syntax error: CREATE TABLE (_id integer primary key autoincrement,username text not null,password text not null);
因此请指定表名。