android studio的sqlite的增删改查代码
时间: 2023-10-14 11:18:06 浏览: 165
我可以为您提供基本的Android Studio中SQLite数据库的增删改查代码示例。首先,您需要在您的项目中创建一个SQLiteOpenHelper类,以便在您的应用程序中打开、创建、升级和维护SQLite数据库。
以下是一个例子:
```java
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "mydatabase.db";
private static final int DATABASE_VERSION = 1;
// Table name and column names
private static final String TABLE_NAME = "mytable";
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_AGE = "age";
// SQL statement to create the table
private static final String SQL_CREATE_TABLE =
"CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_NAME + " TEXT, " +
COLUMN_AGE + " INTEGER)";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// create the table
db.execSQL(SQL_CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Upgrade the database if needed
}
// Insert data into the table
public long insertData(String name, int age) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, name);
values.put(COLUMN_AGE, age);
long result = db.insert(TABLE_NAME, null, values);
db.close();
return result;
}
// Retrieve data from the table
public Cursor getData() {
SQLiteDatabase db = this.getReadableDatabase();
String[] projection = {COLUMN_ID, COLUMN_NAME, COLUMN_AGE};
Cursor cursor = db.query(TABLE_NAME, projection, null, null, null, null, null);
return cursor;
}
// Update a row in the table
public boolean updateData(int id, String name, int age) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, name);
values.put(COLUMN_AGE, age);
String selection = COLUMN_ID + "=?";
String[] selectionArgs = {String.valueOf(id)};
int result = db.update(TABLE_NAME, values, selection, selectionArgs);
db.close();
return result > 0;
}
// Delete a row from the table
public boolean deleteData(int id) {
SQLiteDatabase db = this.getWritableDatabase();
String selection = COLUMN_ID + "=?";
String[] selectionArgs = {String.valueOf(id)};
int result = db.delete(TABLE_NAME, selection, selectionArgs);
db.close();
return result > 0;
}
}
```
您可以将此代码添加到您的项目中,并使用以下代码段执行增删改查操作:
```java
// Instantiate the DatabaseHelper class
DatabaseHelper dbHelper = new DatabaseHelper(this);
// Insert data into the table
dbHelper.insertData("John Doe", 30);
// Retrieve data from the table
Cursor cursor = dbHelper.getData();
if(cursor.moveToFirst()) {
do {
int id = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.COLUMN_ID));
String name = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_NAME));
int age = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.COLUMN_AGE));
Log.d("Database", "ID: " + id + ", Name: " + name + ", Age: " + age);
} while(cursor.moveToNext());
}
cursor.close();
// Update a row in the table
dbHelper.updateData(1, "Jane Doe", 25);
// Delete a row from the table
dbHelper.deleteData(1);
```
请注意,此代码仅供参考,并且可能需要根据您的具体需求进行适当修改。希望能对您有所帮助!
阅读全文