我有一个数据库文件,它有一个特定表和特定数据的插入方法,更新方法,选择方法。 但我有两个不同类型的数据表。 使用该文件使用不同表和数据的最佳方法是什么?
package com.fahimchowdhury.myapplication;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
/**
Created by Misbah Ahmad Chowdhury Fahim on 8/27/2016.
*/
public class DBHelper extends SQLiteOpenHelper{
private static final String DB_NAME = "EXAMDB";
//private static final String ID_COLUMN = "id";
private static final String NAME_COLUMN = "name";
private static final String EMAIL_COLUMN = "email";
private static final String PASS_COLUMN = "password";
private static final String TABLE_NAME = "examTable";
//private static final String DB_VERSION = "dbVersion";
private static final String COMMA = ",";
private static final String PRIMARY_KEY = " PRIMARY KEY";
//private static final String AUTO_INCREMENT = " AUTOINCREMENT";
//private static final String TEXT_TYPE = " TEXT";
//private static final String INTEGER_TYPE = " INTEGER";
private static final String VARCHAR_TYPE = " VARCHAR";
private static final String TABLE_CREATE_QUERY = "CREATE TABLE IF NOT EXISTS "+ TABLE_NAME + "(" +
NAME_COLUMN + VARCHAR_TYPE + COMMA +
EMAIL_COLUMN + VARCHAR_TYPE + PRIMARY_KEY + COMMA +
PASS_COLUMN + VARCHAR_TYPE + ")";
private static final String SELECT_ALL_QUERY = "Select * From " + TABLE_NAME;
private Context context;
private SQLiteDatabase.CursorFactory cursorFactory;
//Constructor, Database can be created/opened by calling this constructor
public DBHelper(Context context, int version){
super(context, DB_NAME, null, version);
this.context = context;
}
public DBHelper(Context context, SQLiteDatabase.CursorFactory cursorFactory, int version){
super(context, DB_NAME, cursorFactory, version);
this.context = context;
this.cursorFactory = cursorFactory;
}
@Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
}
@Override
public void onCreate(SQLiteDatabase dBase) {
try {
//Log.d("Creating_DB_from", "OnCreate");
dBase.execSQL(TABLE_CREATE_QUERY);
} catch (Exception e){
//Log.d("Creating Exception ", "Creating Exception : "+e.getMessage());
}
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
public long insertRow(Info info){
SQLiteDatabase dBase;
try {
dBase = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(NAME_COLUMN, info.getName());
values.put(EMAIL_COLUMN, info.getEmail());
values.put(PASS_COLUMN, info.getPassword());
dBase.insert(TABLE_NAME, null, values);
dBase.close();
return 1;
} catch (SQLiteException ex){
//e.printStackTrace(); //comment printstack before releasing
}
return -1;
}
//selecting all rows from database and returning an arraylist of a class type
public ArrayList<Info> getAllRows(){
SQLiteDatabase dBase;
try{
dBase = this.getReadableDatabase();
ArrayList<Info> profiles = new ArrayList<>();
Info info = new Info();
Cursor cursor = dBase.rawQuery(SELECT_ALL_QUERY, null);
if (cursor.getCount()<=0)
return null;
DataContainer indices = new DataContainer(cursor);
cursor.moveToFirst();
do {
//Setting All property of info object then add to the arraylist
info.setName(cursor.getString(indices.nameColumnIndex));
info.setEmail(cursor.getString(indices.emailColumnIndex));
info.setPassword(cursor.getString(indices.passColumnIndex));
profiles.add(info);
}while (cursor.moveToNext());
cursor.close();
dBase.close();
return profiles;
} catch (SQLException e){
e.printStackTrace(); //comment print stack before releasing
}
return null;
}
//Authorize an user to log in
public int authorizeUser(String email, String userPass){
SQLiteDatabase dBase;
try{
dBase = this.getReadableDatabase();
String[] arg = {email, userPass};
// 1st question mark will be replaced with email and 2nd one with userPass, last parameter "1" is the limit
Cursor cursor = dBase.query(true, TABLE_NAME, null, EMAIL_COLUMN+"=? AND "+ PASS_COLUMN + "=?",
arg, null, null, null, "1");
if(cursor.getCount()>0){
//email password matched
cursor.close();
dBase.close();
return 1;
} else {
cursor.close();
dBase.close();
return 0;
}
} catch (SQLiteException e){
e.printStackTrace(); //comment print stack before releasing
}
return -1;
}
public Info getAuthorizedUser(String email, String userPass){
String[] arg = {email, userPass};
SQLiteDatabase dBase;
try {
dBase = this.getReadableDatabase();
Cursor cursor = dBase.query(true, TABLE_NAME, null, EMAIL_COLUMN+ "=? AND "+ PASS_COLUMN+"=?", arg, null, null, null, "1");
if(cursor.getCount()>0){
//user is found, extract all info(s) from cursor
cursor.moveToFirst();
DataContainer indices = new DataContainer(cursor);
//Make an object of fetched data to return
Info info = new Info(
cursor.getString(indices.nameColumnIndex),
cursor.getString(indices.emailColumnIndex),
cursor.getString(indices.passColumnIndex));
cursor.close();
dBase.close();
return info;
}
} catch (SQLException e){
//e.printStackTrace(); //comment print stack before releasing
}
return null;
}
public Info getAuthorizedUser(String email){
String[] arg = {email};
SQLiteDatabase dBase;
try {
dBase = this.getReadableDatabase();
Cursor cursor = dBase.query(true, TABLE_NAME, null, EMAIL_COLUMN+ "=?", arg, null, null, null, "1");
if(cursor.getCount()>0){
//user is found, extract all info(s) from cursor
cursor.moveToFirst();
DataContainer indices = new DataContainer(cursor);
//Make an object of fetched data to return
Info info = new Info(
cursor.getString(indices.nameColumnIndex),
cursor.getString(indices.emailColumnIndex),
cursor.getString(indices.passColumnIndex));
cursor.close();
dBase.close();
return info;
}
} catch (SQLException e){
//e.printStackTrace(); //comment print stack before releasing
}
return null;
}
private class DataContainer{
int nameColumnIndex;
int emailColumnIndex;
int passColumnIndex;
DataContainer(Cursor cursor){
this.nameColumnIndex = cursor.getColumnIndex(NAME_COLUMN);
this.emailColumnIndex = cursor.getColumnIndex(EMAIL_COLUMN);
this.passColumnIndex = cursor.getColumnIndex(PASS_COLUMN);
}
public int getIndex(Cursor cursor, String columnName){
return cursor.getColumnIndex(columnName);
}
}
}
答案 0 :(得分:0)
你应该使用像插入
这样的新方法 insert_tableName1()
和
insert_tableName2()
如果您有不同类型的数据和不同的参数计数。如果你有相同数量的参数意味着相同类型的数据意味着你可以使用相同的方法,如下面的
insert_table(String tableName, Info info){
dBase = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(NAME_COLUMN, info.getName());
values.put(EMAIL_COLUMN, info.getEmail());
values.put(PASS_COLUMN, info.getPassword());
dBase.insert(tableName, null, values);
dBase.close();
}