android用sqllite数据库实现诗词的增删改查功能
时间: 2023-07-16 17:15:12 浏览: 107
安卓实现对sqlite数据库增删改查操作
5星 · 资源好评率100%
下面是一个简单的示例程序,演示如何在Android应用中使用SQLite数据库实现诗词的增删改查功能。
首先,在你的Android应用中创建一个SQLiteOpenHelper类,该类将用于创建和管理数据库。下面是一个示例代码,你可以根据自己的需要进行修改:
```
public class PoemDbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "poem.db";
private static final int DATABASE_VERSION = 1;
public PoemDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
final String SQL_CREATE_POEM_TABLE = "CREATE TABLE " +
PoemContract.PoemEntry.TABLE_NAME + " (" +
PoemContract.PoemEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
PoemContract.PoemEntry.COLUMN_TITLE + " TEXT NOT NULL, " +
PoemContract.PoemEntry.COLUMN_AUTHOR + " TEXT NOT NULL, " +
PoemContract.PoemEntry.COLUMN_CONTENT + " TEXT NOT NULL" +
");";
db.execSQL(SQL_CREATE_POEM_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + PoemContract.PoemEntry.TABLE_NAME);
onCreate(db);
}
}
```
接下来,我们需要创建一个PoemContract类,该类定义了数据库表的结构。下面是一个示例代码:
```
public class PoemContract {
public static final class PoemEntry implements BaseColumns {
public static final String TABLE_NAME = "poem";
public static final String COLUMN_TITLE = "title";
public static final String COLUMN_AUTHOR = "author";
public static final String COLUMN_CONTENT = "content";
}
}
```
在应用中,我们需要使用ContentValues对象来执行数据库操作。下面是一个示例代码,展示如何添加一首新诗:
```
PoemDbHelper dbHelper = new PoemDbHelper(context);
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(PoemContract.PoemEntry.COLUMN_TITLE, "静夜思");
values.put(PoemContract.PoemEntry.COLUMN_AUTHOR, "李白");
values.put(PoemContract.PoemEntry.COLUMN_CONTENT, "床前明月光,疑是地上霜。");
long newRowId = db.insert(PoemContract.PoemEntry.TABLE_NAME, null, values);
```
要更新一首诗,可以使用以下代码:
```
SQLiteDatabase db = mDbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(PoemContract.PoemEntry.COLUMN_TITLE, "新标题");
String selection = PoemContract.PoemEntry._ID + " = ?";
String[] selectionArgs = { String.valueOf(poemId) };
int count = db.update(
PoemContract.PoemEntry.TABLE_NAME,
values,
selection,
selectionArgs);
```
要删除一首诗,可以使用以下代码:
```
SQLiteDatabase db = mDbHelper.getWritableDatabase();
String selection = PoemContract.PoemEntry._ID + " = ?";
String[] selectionArgs = { String.valueOf(poemId) };
int deletedRows = db.delete(PoemContract.PoemEntry.TABLE_NAME, selection, selectionArgs);
```
最后,要查询诗歌,可以使用以下代码:
```
SQLiteDatabase db = mDbHelper.getReadableDatabase();
String[] projection = {
PoemContract.PoemEntry._ID,
PoemContract.PoemEntry.COLUMN_TITLE,
PoemContract.PoemEntry.COLUMN_AUTHOR,
PoemContract.PoemEntry.COLUMN_CONTENT
};
String sortOrder = PoemContract.PoemEntry.COLUMN_TITLE + " DESC";
Cursor cursor = db.query(
PoemContract.PoemEntry.TABLE_NAME,
projection,
null,
null,
null,
null,
sortOrder
);
while(cursor.moveToNext()) {
long itemId = cursor.getLong(cursor.getColumnIndexOrThrow(PoemContract.PoemEntry._ID));
String title = cursor.getString(cursor.getColumnIndexOrThrow(PoemContract.PoemEntry.COLUMN_TITLE));
String author = cursor.getString(cursor.getColumnIndexOrThrow(PoemContract.PoemEntry.COLUMN_AUTHOR));
String content = cursor.getString(cursor.getColumnIndexOrThrow(PoemContract.PoemEntry.COLUMN_CONTENT));
}
```
上面的代码将返回所有诗歌的标题、作者和内容。你可以根据自己的需要进行修改。
阅读全文