我在以下sqlite数据库代码中收到错误任何人都可以纠正我的错误吗?以下是访问问题的LOGCAT。
在另一个说明中我希望我的用户注册官方电子邮件ID而不是google yahoo或hotmail,我如何限制使用免费电子邮件?
DBHelper
public class DBHelper extends 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 = "emails";
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_EMAILS + " TEXT NOT NULL , "
+ KEY_PASSWORD + " TEXT NOT NULL , " + KEY_FULLNAME
+ " 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 username, String password, String fullnames, String emails) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_USERNAME, username);
initialValues.put(KEY_PASSWORD, password);
initialValues.put(KEY_FULLNAME, fullnames);
initialValues.put(KEY_EMAILS, emails);
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;
}
}
会员注册活动
public class MembersRegister extends Activity {
DBAdapter dbAdapter;
EditText txtUserName, fullname, authcodes, txtemail;
EditText txtPassword;
Button btnLogin;
Button btnRegister;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
authcodes = (EditText) findViewById(R.id.auth_code);
fullname = (EditText) findViewById(R.id.registerName);
txtUserName = (EditText) findViewById(R.id.userName);
txtPassword = (EditText) findViewById(R.id.registerPassword);
txtemail = (EditText) findViewById(R.id.registerEmail);
btnRegister = (Button) findViewById(R.id.btnRegister);
dbAdapter = new DBAdapter(this);
dbAdapter.open();
btnRegister.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);
imm.hideSoftInputFromWindow(fullname.getWindowToken(), 0);
imm.hideSoftInputFromWindow(txtemail.getWindowToken(), 0);
try {
String username = txtUserName.getText().toString();
String password = txtPassword.getText().toString();
String fullnames = fullname.getText().toString();
String emails = txtemail.getText().toString();
long i = dbAdapter.register(username, password, fullnames,
emails);
if (i != -1)
Toast.makeText(MembersRegister.this,
"You have successfully registered",
Toast.LENGTH_LONG).show();
} catch (SQLException e) {
Toast.makeText(MembersRegister.this, "Some problem occurred",
Toast.LENGTH_LONG).show();
}
}
});
}
}
logcat的
09-30 10:34:00.807: E/Database(368): Error inserting username=hello emails=hello password=hello fullname=hello
09-30 10:34:00.807: E/Database(368): android.database.sqlite.SQLiteException: table members has no column named emails: , while compiling: INSERT INTO members(username, emails, password, fullname) VALUES(?, ?, ?, ?);
09-30 10:34:00.807: E/Database(368): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
09-30 10:34:00.807: E/Database(368): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
09-30 10:34:00.807: E/Database(368): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
09-30 10:34:00.807: E/Database(368): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
09-30 10:34:00.807: E/Database(368): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41)
09-30 10:34:00.807: E/Database(368): at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1149)
09-30 10:34:00.807: E/Database(368): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1569)
09-30 10:34:00.807: E/Database(368): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1426)
09-30 10:34:00.807: E/Database(368): at com.ideals.ideal.DBAdapter.register(DBAdapter.java:42)
09-30 10:34:00.807: E/Database(368): at com.ideals.ideal.MembersRegister$1.onClick(MembersRegister.java:50)
09-30 10:34:00.807: E/Database(368): at android.view.View.performClick(View.java:2485)
09-30 10:34:00.807: E/Database(368): at android.view.View$PerformClick.run(View.java:9080)
09-30 10:34:00.807: E/Database(368): at android.os.Handler.handleCallback(Handler.java:587)
09-30 10:34:00.807: E/Database(368): at android.os.Handler.dispatchMessage(Handler.java:92)
09-30 10:34:00.807: E/Database(368): at android.os.Looper.loop(Looper.java:123)
09-30 10:34:00.807: E/Database(368): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-30 10:34:00.807: E/Database(368): at java.lang.reflect.Method.invokeNative(Native Method)
09-30 10:34:00.807: E/Database(368): at java.lang.reflect.Method.invoke(Method.java:507)
09-30 10:34:00.807: E/Database(368): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-30 10:34:00.807: E/Database(368): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-30 10:34:00.807: E/Database(368): at dalvik.system.NativeStart.main(Native Method)
提前致谢
答案 0 :(得分:0)
您的create SQL以CREATE TABLE IF NOT EXISTS开头。所以我的想法是你在设备已经拥有该表时添加了电子邮件列。如果是这种情况,那么只需重新安装应用程序 - 它将删除数据库,因此您可以确保将使用该列创建表。
答案 1 :(得分:0)
删除并创建“成员”表。也许你的商店里有旧版本。
答案 2 :(得分:0)
您需要卸载您的应用程序再次安装它。 onCreate
方法仅称为一个方法。要触发onUpgrade
,您需要增加数据库编号。
此外,您可能不希望删除onUpgrade
中的表格,因为用户将丢失所有数据。
答案 3 :(得分:0)
从模拟器中卸载应用程序并再次运行。
答案 4 :(得分:0)
可能是您应该更改数据库名称
来自这个
private static final String DATABASE_NAME = "membersdb";
到
private static final String DATABASE_NAME = "members.db";