android用sqllite数据库实现古诗的增、删、改、查,写一个完整的简单示例,写清文件名
时间: 2023-07-10 10:03:43 浏览: 174
好的,以下是一个简单的 Android SQLite 数据库示例,实现了对古诗的增、删、改、查操作。主要分为两个部分:数据库操作类 `PoemDBHelper` 和具体的增删改查操作实现。
1. `PoemDBHelper.java` 文件:
```java
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class PoemDBHelper extends SQLiteOpenHelper {
// 数据库名称和版本号
private static final String DB_NAME = "poem.db";
private static final int DB_VERSION = 1;
// 表名和列名常量
public static final String TABLE_NAME = "poem";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_TITLE = "title";
public static final String COLUMN_AUTHOR = "author";
public static final String COLUMN_CONTENT = "content";
// 创建表的 SQL 语句
private static final String CREATE_TABLE_SQL = "CREATE TABLE " + TABLE_NAME + " ("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_TITLE + " TEXT, "
+ COLUMN_AUTHOR + " TEXT, "
+ COLUMN_CONTENT + " TEXT)";
public PoemDBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// 创建表
db.execSQL(CREATE_TABLE_SQL);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// 数据库升级时调用
// 这里我们简单处理,直接删除表并重新创建
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
```
2. 具体的增删改查操作实现:
```java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
public class PoemDAO {
private PoemDBHelper dbHelper;
public PoemDAO(Context context) {
dbHelper = new PoemDBHelper(context);
}
// 增加古诗
public void addPoem(Poem poem) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(PoemDBHelper.COLUMN_TITLE, poem.getTitle());
values.put(PoemDBHelper.COLUMN_AUTHOR, poem.getAuthor());
values.put(PoemDBHelper.COLUMN_CONTENT, poem.getContent());
db.insert(PoemDBHelper.TABLE_NAME, null, values);
db.close();
}
// 删除古诗
public void deletePoem(Poem poem) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete(PoemDBHelper.TABLE_NAME, PoemDBHelper.COLUMN_ID + "=?", new String[]{String.valueOf(poem.getId())});
db.close();
}
// 修改古诗
public void updatePoem(Poem poem) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(PoemDBHelper.COLUMN_TITLE, poem.getTitle());
values.put(PoemDBHelper.COLUMN_AUTHOR, poem.getAuthor());
values.put(PoemDBHelper.COLUMN_CONTENT, poem.getContent());
db.update(PoemDBHelper.TABLE_NAME, values, PoemDBHelper.COLUMN_ID + "=?", new String[]{String.valueOf(poem.getId())});
db.close();
}
// 查找所有古诗
public List<Poem> getAllPoems() {
List<Poem> poems = new ArrayList<>();
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query(PoemDBHelper.TABLE_NAME, null, null, null, null, null, null);
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex(PoemDBHelper.COLUMN_ID));
String title = cursor.getString(cursor.getColumnIndex(PoemDBHelper.COLUMN_TITLE));
String author = cursor.getString(cursor.getColumnIndex(PoemDBHelper.COLUMN_AUTHOR));
String content = cursor.getString(cursor.getColumnIndex(PoemDBHelper.COLUMN_CONTENT));
poems.add(new Poem(id, title, author, content));
}
cursor.close();
db.close();
return poems;
}
// 根据标题查找古诗
public List<Poem> getPoemsByTitle(String title) {
List<Poem> poems = new ArrayList<>();
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query(PoemDBHelper.TABLE_NAME, null, PoemDBHelper.COLUMN_TITLE + " like ?", new String[]{"%" + title + "%"}, null, null, null);
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex(PoemDBHelper.COLUMN_ID));
String poemTitle = cursor.getString(cursor.getColumnIndex(PoemDBHelper.COLUMN_TITLE));
String author = cursor.getString(cursor.getColumnIndex(PoemDBHelper.COLUMN_AUTHOR));
String content = cursor.getString(cursor.getColumnIndex(PoemDBHelper.COLUMN_CONTENT));
poems.add(new Poem(id, poemTitle, author, content));
}
cursor.close();
db.close();
return poems;
}
}
```
3. `Poem.java` 文件,表示古诗实体类:
```java
public class Poem {
private int id;
private String title;
private String author;
private String content;
public Poem(int id, String title, String author, String content) {
this.id = id;
this.title = title;
this.author = author;
this.content = content;
}
public Poem(String title, String author, String content) {
this.title = title;
this.author = author;
this.content = content;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
```
4. `MainActivity.java` 文件,测试代码:
```java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PoemDAO dao = new PoemDAO(this);
// 添加古诗
dao.addPoem(new Poem("静夜思", "李白", "床前明月光,疑是地上霜。举头望明月,低头思故乡。"));
// 查询所有古诗
List<Poem> poems = dao.getAllPoems();
for (Poem poem : poems) {
Log.i("MainActivity", "id: " + poem.getId() + ", title: " + poem.getTitle() + ", author: " + poem.getAuthor() + ", content: " + poem.getContent());
}
// 修改古诗
Poem poemToUpdate = poems.get(0);
poemToUpdate.setTitle("静夜思(改)");
dao.updatePoem(poemToUpdate);
// 查询所有古诗
poems = dao.getAllPoems();
for (Poem poem : poems) {
Log.i("MainActivity", "id: " + poem.getId() + ", title: " + poem.getTitle() + ", author: " + poem.getAuthor() + ", content: " + poem.getContent());
}
// 根据标题查询古诗
List<Poem> poemsByTitle = dao.getPoemsByTitle("夜思");
for (Poem poem : poemsByTitle) {
Log.i("MainActivity", "id: " + poem.getId() + ", title: " + poem.getTitle() + ", author: " + poem.getAuthor() + ", content: " + poem.getContent());
}
// 删除古诗
Poem poemToDelete = poems.get(0);
dao.deletePoem(poemToDelete);
// 查询所有古诗
poems = dao.getAllPoems();
for (Poem poem : poems) {
Log.i("MainActivity", "id: " + poem.getId() + ", title: " + poem.getTitle() + ", author: " + poem.getAuthor() + ", content: " + poem.getContent());
}
}
}
```
注意,由于我们使用了 SQLiteOpenHelper 类,因此需要在 AndroidManifest.xml 中添加以下权限:
```xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
```
同时,我们需要在 `<application>` 标签下添加以下代码,以便在应用启动时进行数据库初始化:
```xml
<meta-data
android:name="android.database.sqlite.SQLiteDatabase"
android:value="PoemDBHelper.getWritableDatabase()" />
```
好了,这就是一个简单的 Android SQLite 数据库示例啦!
阅读全文