基于Android实现SQLiteOpenHelper的继承类MyHelper,创建数据表user,字段包括自增id、username、password等三个字段,并插入三条记录. 在MainActivity类中实现对数据库增删改查操作,并在用查询或者命令行的方法观察数据库的变化。
时间: 2024-03-22 18:38:37 浏览: 54
Android sqlite的一些基本操作,包括数据库创建、数据库版本升级、创建表、数据的增删改查-IT计算机-毕业设计.zip
好的,我会尽力回答你的问题。以下是基于Android实现SQLiteOpenHelper的继承类MyHelper,创建数据表user,字段包括自增id、username、password等三个字段,并插入三条记录的代码:
```java
public class MyHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "my_database";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "user";
private static final String COLUMN_ID = "id";
private static final String COLUMN_USERNAME = "username";
private static final String COLUMN_PASSWORD = "password";
private static final String SQL_CREATE_TABLE =
"CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
COLUMN_USERNAME + " TEXT," +
COLUMN_PASSWORD + " TEXT)";
public MyHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_TABLE);
ContentValues values = new ContentValues();
values.put(COLUMN_USERNAME, "user1");
values.put(COLUMN_PASSWORD, "password1");
db.insert(TABLE_NAME, null, values);
values.clear();
values.put(COLUMN_USERNAME, "user2");
values.put(COLUMN_PASSWORD, "password2");
db.insert(TABLE_NAME, null, values);
values.clear();
values.put(COLUMN_USERNAME, "user3");
values.put(COLUMN_PASSWORD, "password3");
db.insert(TABLE_NAME, null, values);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
```
在MainActivity类中实现对数据库增删改查操作,可以在onCreate方法中获取数据库实例并进行操作,如下所示:
```java
public class MainActivity extends AppCompatActivity {
private MyHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new MyHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
// 增加数据
ContentValues values = new ContentValues();
values.put(MyHelper.COLUMN_USERNAME, "user4");
values.put(MyHelper.COLUMN_PASSWORD, "password4");
db.insert(MyHelper.TABLE_NAME, null, values);
// 删除数据
String selection = MyHelper.COLUMN_USERNAME + "=?";
String[] selectionArgs = {"user1"};
db.delete(MyHelper.TABLE_NAME, selection, selectionArgs);
// 更新数据
values.clear();
values.put(MyHelper.COLUMN_PASSWORD, "new_password");
String whereClause = MyHelper.COLUMN_USERNAME + "=?";
String[] whereArgs = {"user2"};
db.update(MyHelper.TABLE_NAME, values, whereClause, whereArgs);
// 查询数据
String[] projection = {
MyHelper.COLUMN_ID,
MyHelper.COLUMN_USERNAME,
MyHelper.COLUMN_PASSWORD
};
Cursor cursor = db.query(MyHelper.TABLE_NAME, projection, null, null, null, null, null);
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex(MyHelper.COLUMN_ID));
String username = cursor.getString(cursor.getColumnIndex(MyHelper.COLUMN_USERNAME));
String password = cursor.getString(cursor.getColumnIndex(MyHelper.COLUMN_PASSWORD));
Log.d("MainActivity", "id:" + id + ", username:" + username + ", password:" + password);
}
cursor.close();
}
@Override
protected void onDestroy() {
super.onDestroy();
dbHelper.close();
}
}
```
其中,增加数据使用db.insert方法,删除数据使用db.delete方法,更新数据使用db.update方法,查询数据使用db.query方法。最后在onDestroy方法中关闭数据库。你可以通过命令行或者Android Studio的Database Inspector来观察数据库的变化。
阅读全文