You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.6 KiB

package com.example.register.dateoperation;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import androidx.annotation.Nullable;
public class Database extends SQLiteOpenHelper {
public Database(@Nullable Context context) {
super(context, "orange", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//创建用户表
String sql = "create table orange_user(id integer primary key autoincrement, username varchar(50), password varchar(50),sex varchar(10),city carchar(50))";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
/**
* 插入数据
*
* @param sqLiteDatabase
* @param username
* @param password
* @param sex
* @param city
*/
public void insertUser(SQLiteDatabase sqLiteDatabase, String username, String password, String sex, String city) {
ContentValues contentValues = new ContentValues();
contentValues.put("username", username);
contentValues.put("password", password);
contentValues.put("sex", sex);
contentValues.put("city", city);
sqLiteDatabase.insert("orange_user", null, contentValues);
sqLiteDatabase.close();
}
/**
* 更新数据
*/
public void update(SQLiteDatabase sqLiteDatabase,int id,String username,String passwd,String sex,int age){
//创建一个ContentValues对象
ContentValues values = new ContentValues();
//以键值对的形式插入
values.put("username",username);
values.put("passwd",passwd);
values.put("sex",sex);
values.put("age",age);
//执行修改方法
sqLiteDatabase.update("user",values,"id=?",new String[]{id+""} );
sqLiteDatabase.close();
}
/**
* 查询数据
*
* @param sqLiteDatabase
* @param bundle
* @return
*/
public Bundle queryUserInfo(SQLiteDatabase sqLiteDatabase, Bundle bundle) {
String username = bundle.getString("username");
Cursor cursor = sqLiteDatabase.rawQuery("select * from orange_user where username=?", new String[]{username});
if (cursor != null) {
while (cursor.moveToNext()) {
bundle.putString("sex", cursor.getString(3));
bundle.putString("city", cursor.getString(4));
}
}
cursor.close();
sqLiteDatabase.close();
return bundle;
}
}