如何使用android studio sqlite中其他表中的列创建视图?
答案 0 :(得分:0)
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "user_detail.db";
public DBHelper(Context context) {
super(context,DATABASE_NAME,null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + UserProfile.User.TABLE_NAME + "("+UserProfile.User.col_1+" Integer primary key autoincrement," +
"" +UserProfile.User.col_2 + " text," +
""+UserProfile.User.col_3+ " text," +
""+UserProfile.User.col_4+ " text," +
""+UserProfile.User.col_5+ " text )");
}
@Override
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion) {
db.execSQL(("drop table if exists " + UserProfile.User.TABLE_NAME));
onCreate(db);
}
public boolean addInfo(String userName,String password){
SQLiteDatabase db= this.getWritableDatabase();
ContentValues con=new ContentValues();
//con.put(col_1,ID);
con.put(UserProfile.User.col_2,userName);
con.put(UserProfile.User.col_5,password);
//con.put(col_3,dateOfBirth);
//con.put(col_4,Gender);
long result=db.insert(UserProfile.User.TABLE_NAME,null,con);
db.close();
if(result==-1){
return false;
}else{
return true;
}
}
public boolean updateInfor(String username,String dateOfBirth,String gender, String password){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues con= new ContentValues();
//con.put(UserProfile.User.col_1,userId);
con.put(UserProfile.User.col_2,username);
con.put(UserProfile.User.col_3,dateOfBirth);
con.put(UserProfile.User.col_5,password);
con.put(UserProfile.User.col_4,gender);
db.update(UserProfile.User.TABLE_NAME,con,UserProfile.User.col_2+"=?",new String[]{String.valueOf(username)});
// db.close();
return true;
}
public Integer DeleteInfor(String userName){
SQLiteDatabase db=this.getReadableDatabase();
return db.delete(UserProfile.User.TABLE_NAME,UserProfile.User.col_2+"=?",new String[]{userName});
}
public Cursor getData(){
SQLiteDatabase db= this.getWritableDatabase();
Cursor res=db .rawQuery(("select * from "+UserProfile.User.TABLE_NAME ),null);
return res;
}
public List<User> getAllUser(String users){
String[] columns={
UserProfile.User.col_1,
UserProfile.User.col_2,
UserProfile.User.col_3,
UserProfile.User.col_4,
UserProfile.User.col_5,
};
String sortOrder=UserProfile.User.col_1 +"ASC";
List<User>userList = new ArrayList <User>();
SQLiteDatabase db= this.getReadableDatabase();
Cursor cursor = db.query(UserProfile.User.TABLE_NAME,
columns,
null,
null,
null,
null,
sortOrder);
if(cursor.moveToFirst()){
do{
User user= new User();
user.setUserId(cursor.getString(cursor.getColumnIndex(UserProfile.User.col_1)));
user.setUserName(cursor.getString(cursor.getColumnIndex(UserProfile.User.col_2)));
user.setDateOfBirth(cursor.getString(cursor.getColumnIndex(UserProfile.User.col_3)));
user.setGender(cursor.getString(cursor.getColumnIndex(UserProfile.User.col_4)));
user.setPassword((cursor.getString(cursor.getColumnIndex(UserProfile.User.col_5))));
userList.add(user);
}while (cursor.moveToNext());
}
cursor.close();
db.close();
return userList;
}
public boolean checkUser(String username){
String[] columns={UserProfile.User.col_1};
SQLiteDatabase db=this.getReadableDatabase();
String selection = UserProfile.User.col_2+"=?";
String [] selectionArgs={username};
Cursor cursor=db.query(UserProfile.User.TABLE_NAME,
columns,
selection,
selectionArgs,
null,
null,
null);
int cursorCount=cursor.getCount();
cursor.close();
db.close();
if(cursorCount>0){
return true;
}
return false;
}
public boolean checkUsers(String username,String password) {
String[] columns={UserProfile.User.col_1};
SQLiteDatabase db=this.getReadableDatabase();
String selection = UserProfile.User.col_2+"=? and "+UserProfile.User.col_5 +"=?";
String [] selectionArgs={username,password};
Cursor cursor=db.query(UserProfile.User.TABLE_NAME,
columns,
selection,
selectionArgs,
null,
null,
null);
int cursorCount=cursor.getCount();
cursor.close();
db.close();
if(cursorCount>0){
return true;
}
return false;
}
答案 1 :(得分:0)
~/mydir/a.bat
~/mydir/a.sh
~/mydir/a-lib/a-1.0.0.jar
~/mydir/a-lib/a-dep1-1.2.3.jar
~/mydir/a-lib/a-dep2-4.5.6.jar
~/mydir/b.bat
~/mydir/b.sh
~/mydir/b-lib/b-1.0.0.jar
答案 2 :(得分:-1)
使用SQLite Database Helper类
public class SQLiteHandler extends SQLiteOpenHelper {
private static final String TAG = SQLiteHandler.class.getSimpleName();
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "android_api";
// Login table name
private static final String TABLE_USER = "user";
// Login Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_EMAIL = "email";
private static final String KEY_UID = "uid";
private static final String KEY_CREATED_AT = "created_at";
public SQLiteHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_USER + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_EMAIL + " TEXT UNIQUE," + KEY_UID + " TEXT,"
+ KEY_CREATED_AT + " TEXT" + ")";
db.execSQL(CREATE_LOGIN_TABLE);
Log.d(TAG, "Database tables created");
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);
// Create tables again
onCreate(db);
}
}