我已经使用一些教程编写了登录功能以存储用户名和密码。它工作正常。一段时间后,我需要在单个数据库中管理两个表。我通过关注这个网站来实现这一点。
但我得到没有这样的表错误。任何帮助,将不胜感激。这是我的代码:
public class LoginDataBaseAdapter{
public static final String ROW_ID = "_id";
public static final String USERNAME = "name";
public static final String PASSWORD = "password";
private static final String DATABASE_TABLE = "users";
public SQLiteDatabase db;
// Context of the application using the database.
private final Context context;
private DatabaseHelper dbHelper;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DatabaseAdapter.DATABASE_NAME,null, DatabaseAdapter.DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
public LoginDataBaseAdapter(Context _context)
{
this.context = _context;
}
public LoginDataBaseAdapter open() throws SQLException
{
this.dbHelper = new DatabaseHelper(this.context);
this.db = this.dbHelper.getWritableDatabase();
return this;
}
public void close()
{
dbHelper.close();
}
public void createUsers(String name, String pass){
ContentValues initialValues = new ContentValues();
initialValues.put(USERNAME, name);
initialValues.put(PASSWORD, pass);
db.insert(DATABASE_TABLE, null, initialValues);
}
public boolean deleteUser(long rowId) {
return this.db.delete(DATABASE_TABLE, ROW_ID + "=" + rowId, null) > 0;
}
public Cursor getAllUsers() {
return this.db.query(DATABASE_TABLE, new String[] { ROW_ID,
USERNAME, PASSWORD }, null, null, null, null, null);
}
public String getUser(String username) throws SQLException {
Cursor cursor=db.query(DATABASE_TABLE, 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 updateUser(long rowId, String name, String pass,
String year){
ContentValues args = new ContentValues();
args.put(USERNAME, name);
args.put(PASSWORD, pass);
return this.db.update(DATABASE_TABLE, args, ROW_ID + "=" + rowId, null) >0;
}
public SQLiteDatabase getDatabaseInstance()
{
return db;
}
/*public void insertEntry(String userName,String password)
{
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put("USERNAME", userName);
newValues.put("PASSWORD",password);
// Insert the row into your table
db.insert("LOGIN", null, newValues);
///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
}
public int deleteEntry(String UserName)
{
//String id=String.valueOf(ID);
String where="USERNAME=?";
int numberOFEntriesDeleted= db.delete("LOGIN", where, new String[]{UserName}) ;
// Toast.makeText(context, "Number fo Entry Deleted Successfully : "+numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
return numberOFEntriesDeleted;
} */
/* public String getSinlgeEntry(String userName)
{
Cursor cursor=db.query("LOGIN", 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 void updateEntry(String userName,String password)
{
// Define the updated row content.
ContentValues updatedValues = new ContentValues();
// Assign values for each row.
updatedValues.put("USERNAME", userName);
updatedValues.put("PASSWORD",password);
String where="USERNAME = ?";
db.update("LOGIN",updatedValues, where, new String[]{userName});
}
*/
}
这是我的适配器类:
public class DatabaseAdapter {
// public static final String DATABASE_NAME = "example";
public static final int DATABASE_VERSION = 1;
private static final String CREATE_TABLE_USERS =
"create table users (_id integer primary key autoincrement, "
+ LoginDataBaseAdapter.USERNAME+ " TEXT,"
+ LoginDataBaseAdapter.PASSWORD+ " TEXT,"
+ ");";
private static final String CREATE_TABLE_REMIND = "create table remind (_id integer primary key autoincrement, " //$NON-NLS-1$
+MedicationDBAdapter.MEDICINAME+" TEXT,"
+MedicationDBAdapter.MEDICINEDIR+" TEXT,"
+ ");";
public static final String DATABASE_NAME="example.db";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DatabaseAdapter(Context ctx)
{
this.context = ctx;
this.DBHelper = new DatabaseHelper(this.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(CREATE_TABLE_USERS);
db.execSQL(CREATE_TABLE_REMIN);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion)
{
// Adding any table mods to this guy here
}
}
/**
* open the db
* @return this
* @throws SQLException
* return type: DBAdapter
*/
public DatabaseAdapter open() throws SQLException
{
this.db = this.DBHelper.getWritableDatabase();
return this;
}
/**
* close the db
* return type: void
*/
public void close()
{
this.DBHelper.close();
}
}