【问题标题】:Using multiple tables in SQLite database android在SQLite数据库android中使用多个表
【发布时间】:2014-10-28 09:40:41
【问题描述】:

我正在 android 中创建一个信息系统,首先登录,然后它要求用户显示员工列表并添加新员工。所以问题是如何使用不同的表来存储员工的信息并将其显示在列表视图的其他活动中。这是我的代码

这是数据库的适配器类。

公共类 LoginDataBaseAdapter {

static final String DATABASE_NAME = "login.db";
static final int DATABASE_VERSION = 1;
public static final int NAME_COLUMN = 1;

static final String DATABASE_CREATE = "create table "+"LOGIN"+
        "( " +"ID"+" integer primary key autoincrement,"+ "USERNAME  text,PASSWORD text); ";
public  SQLiteDatabase db;
private final Context context;

private DbHelper dbHelper;
public  LoginDataBaseAdapter(Context _context) 
{
    context = _context;
    dbHelper = new DbHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public  LoginDataBaseAdapter open() throws SQLException 
{
    db = dbHelper.getWritableDatabase();
    return this;
}
public void close() 
{
    db.close();
}
public  SQLiteDatabase getDatabaseInstance()
{
    return db;
}

public void insertEntry(String userName,String password)
{
   ContentValues newValues = new ContentValues();
   newValues.put("USERNAME", userName);
    newValues.put("PASSWORD",password);

    db.insert("LOGIN", null, newValues);

}
public int deleteEntry(String UserName)
{

    String where="USERNAME=?";
    int numberOFEntriesDeleted= db.delete("LOGIN", where, new String[]{UserName}) ;
    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)
{
    ContentValues updatedValues = new ContentValues();

    updatedValues.put("USERNAME", userName);
    updatedValues.put("PASSWORD",password);

    String where="USERNAME = ?";
    db.update("LOGIN",updatedValues, where, new String[]{userName});               
}       

}

这是辅助类。

公共类 DbHelper 扩展 SQLiteOpenHelper {

public DbHelper(Context context, String name, CursorFactory factory,
        int version) {
    super(context, name, factory, version);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase _db) {
    // TODO Auto-generated method stub
    _db.execSQL(LoginDataBaseAdapter.DATABASE_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) {

    // TODO Auto-generated method stub
    Log.w("TaskDBAdapter", "Upgrading from version " + _oldVersion + " to " + _newVersion + ", which will destroy all old data");

    _db.execSQL("DROP TABLE IF EXISTS " + "TEMPLATE");
    onCreate(_db);
}

}

【问题讨论】:

    标签: java android database sqlite android-activity


    【解决方案1】:

    您可以在此数据库中创建另一个表,我使用您提供的相同字符串根据您的需要编辑它

    static final String Table2 = "create table "+"LOGIN"+
        "( " +"ID"+" integer primary key autoincrement,"+ "USERNAME  text,PASSWORD text); ";
    

    现在在你的帮助类的 onCreate() 方法中执行这个查询和之前的查询

    @Override
    public void onCreate(SQLiteDatabase _db) {
    // TODO Auto-generated method stub
     _db.execSQL(LoginDataBaseAdapter.DATABASE_CREATE);
    _db.execSQL(LoginDataBaseAdapter.Table2);
      }
    

    然后使用 helper 类从这个表中添加和检索数据。查看这个链接以了解在 android 中从表中插入和检索数据 http://hmkcode.com/android-simple-sqlite-database-tutorial/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-02
      • 2023-03-08
      • 2015-07-12
      • 1970-01-01
      • 2021-06-02
      • 2018-02-24
      相关资源
      最近更新 更多