我正在创建一个接受用户注册信息的应用,但我收到的错误是“没有这样的表:UserAccount”,下面是我的代码:
我的DBHelper
public class DBAdapter
{
public static final String KEY_ROWID = "_id";
public static final String KEY_TITLE = "title";
public static final String KEY_FULLNAME = "fullname";
public static final String KEY_EMAIL = "email";
public static final String KEY_PASS = "password";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "pioneer.db";
private static final int DATABASE_VERSION = 1;
public static final String DATABASE_TABLE_NAME = "UserAccount";
private static final String DATABASE_CREATE =
"create table IF NOT EXISTS users (_id integer primary key autoincrement, "
+ "title text, fullname text, email text, password text);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
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);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion
+ " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS users");
onCreate(db);
}
}
//---opens the database---
public DBAdapter opendb() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//---insert a title into the database---
public long insertUser(String title, String fullname,String email, String Password)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_FULLNAME, fullname);
initialValues.put(KEY_EMAIL, email);
initialValues.put(KEY_PASS, Password);
return db.insert(DATABASE_TABLE_NAME, null, initialValues);
}
//---deletes a particular title---
public boolean deleteTitle(long rowId)
{
return db.delete(DATABASE_TABLE_NAME, KEY_ROWID +
"=" + rowId, null) > 0;
}
//---retrieves all the titles---
public Cursor getAllUsers()
{
return db.query(DATABASE_TABLE_NAME, new String[] {
KEY_ROWID,
KEY_TITLE,
KEY_FULLNAME,
KEY_EMAIL,
KEY_PASS,
},
null,
null,
null,
null,
null);
}
//---retrieves a particular user---
public Cursor getUser(long rowId) throws SQLException
{
Cursor mCursor =
db.query(DATABASE_TABLE_NAME, new String[] {
KEY_ROWID,
KEY_TITLE,
KEY_FULLNAME,
KEY_EMAIL,
KEY_PASS,
},
KEY_ROWID + "=" + rowId,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
//---updates a title---
public boolean updateTitle(long rowId, String title, String fullname,String email, String Password)
{
ContentValues args = new ContentValues();
args.put(KEY_TITLE, title);
args.put(KEY_FULLNAME, fullname);
args.put(KEY_EMAIL, email);
args.put(KEY_PASS, Password);
return db.update(DATABASE_TABLE_NAME, args,
KEY_ROWID + "=" + rowId, null) > 0;
}
}
这就是我使用它的地方
public class RegisterActivity extends Activity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
final EditText full_Name = (EditText)findViewById(R.id.reg_fullname);
final EditText mMail = (EditText)findViewById(R.id.reg_email);
final EditText mPass = (EditText)findViewById(R.id.reg_password);
final TextView loginScreen = (TextView) findViewById(R.id.link_to_login);
btn = (Button)findViewById(R.id.btnRegister);
final DBAdapter db = new DBAdapter(this);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (db !=null){
db.opendb();
long uid =db.insertUser("Mr", full_Name.getText().toString(), mMail.getText().toString(), mPass.getText().toString());
Cursor c = db.getUser(uid);
if (c !=null){
ShowMessage(c.getString(2));
}
}
db.close();
}
});
}
private void ShowMessage(String message){
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
// Listening to Login Screen link
/* loginScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Closing registration screen
// Switching to Login Screen/closing register screen
finish();
}
});*/
@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_register, menu);
return true;
}
}
任何帮助将不胜感激。
答案 0 :(得分:2)
您正在创建一个名称(users
)与查询语句中使用的名称不同的表(UserAccount
)..
public static final String DATABASE_TABLE_NAME = "UserAccount";
private static final String DATABASE_CREATE =
"create table IF NOT EXISTS users (_id integer primary key autoincrement, "
+ "title text, fullname text, email text, password text);";
它应该是:
private static final String DATABASE_CREATE =
"create table IF NOT EXISTS "+ DATABASE_TABLE_NAME+" (_id integer primary key autoincrement, "
+ "title text, fullname text, email text, password text);";
答案 1 :(得分:1)
public static final String DATABASE_TABLE_NAME = "UserAccount";
...
"create table IF NOT EXISTS users
users
和UserAccount
不同。你应该让它们相同才能发挥作用。
答案 2 :(得分:1)
users!= UserAccounts。
您可以创建表用户,但可以在UserAccounts中查询。
答案 3 :(得分:1)
您的DATABASE_CREATE会使“用户”表格成为DATABASE_TABLE_NAME使用的“UserAccount”表格。将变量添加到sql表创建字符串。
答案 4 :(得分:0)
将创建查询更改为
private static final String DATABASE_CREATE =
"create table IF NOT EXISTS "+ DATABASE_TABLE_NAME+" (_id integer primary key autoincrement, "
+ "title text, fullname text, email text, password text);";