用android中的sqllite数据库写一个关于古诗的增、删、查、改功能的笔记本功能,请写一个详细的包括java文件和较完善修饰、布局文件的示例
时间: 2024-02-11 20:04:18 浏览: 104
androdi简单操作数据库入门.txt
好的,我会尽量详细地回答你的问题。
首先,我们需要创建一个数据库来存储古诗的相关信息,包括题目、作者、朝代、内容等。我们可以在 `SQLiteOpenHelper` 类中实现数据库的创建和更新。
下面是一个示例的 `PoemDatabaseHelper` 类:
```java
public class PoemDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "poems.db";
private static final int DATABASE_VERSION = 1;
// 表名和列名
public static final String TABLE_NAME = "poems";
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_DYNASTY = "dynasty";
public static final String COLUMN_CONTENT = "content";
// 创建表格的 SQL 语句
private static final String CREATE_TABLE =
"CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_TITLE + " TEXT, " +
COLUMN_AUTHOR + " TEXT, " +
COLUMN_DYNASTY + " TEXT, " +
COLUMN_CONTENT + " TEXT);";
public PoemDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// 升级数据库的逻辑
}
}
```
在这个类中,我们定义了数据库的名称和版本号,以及我们需要存储的表格的名称和列名。`CREATE_TABLE` 字符串是创建表格的 SQL 语句。`onCreate` 方法在数据库第一次创建时被调用,我们在其中执行创建表格的操作。`onUpgrade` 方法用于升级数据库,在这个示例中我们留空。
接下来,我们需要创建一个 `Poem` 类来表示古诗的信息:
```java
public class Poem {
private long id;
private String title;
private String author;
private String dynasty;
private String content;
public Poem() {}
public Poem(long id, String title, String author, String dynasty, String content) {
this.id = id;
this.title = title;
this.author = author;
this.dynasty = dynasty;
this.content = content;
}
// getter 和 setter 方法
public long getId() {
return id;
}
public void setId(long 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 getDynasty() {
return dynasty;
}
public void setDynasty(String dynasty) {
this.dynasty = dynasty;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
```
这个类中包含了古诗的各项信息,以及 getter 和 setter 方法。注意,我们需要有一个 `id` 字段来唯一标识每一首古诗。
接下来,我们可以创建一个 `PoemDataSource` 类来对数据库进行操作:
```java
public class PoemDataSource {
private SQLiteDatabase database;
private PoemDatabaseHelper dbHelper;
public PoemDataSource(Context context) {
dbHelper = new PoemDatabaseHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
// 插入一条数据
public Poem insertPoem(String title, String author, String dynasty, String content) {
ContentValues values = new ContentValues();
values.put(PoemDatabaseHelper.COLUMN_TITLE, title);
values.put(PoemDatabaseHelper.COLUMN_AUTHOR, author);
values.put(PoemDatabaseHelper.COLUMN_DYNASTY, dynasty);
values.put(PoemDatabaseHelper.COLUMN_CONTENT, content);
long insertId = database.insert(PoemDatabaseHelper.TABLE_NAME, null, values);
Cursor cursor = database.query(PoemDatabaseHelper.TABLE_NAME,
new String[] { PoemDatabaseHelper.COLUMN_ID, PoemDatabaseHelper.COLUMN_TITLE,
PoemDatabaseHelper.COLUMN_AUTHOR, PoemDatabaseHelper.COLUMN_DYNASTY,
PoemDatabaseHelper.COLUMN_CONTENT },
PoemDatabaseHelper.COLUMN_ID + " = " + insertId, null,
null, null, null);
cursor.moveToFirst();
Poem newPoem = cursorToPoem(cursor);
cursor.close();
return newPoem;
}
// 删除一条数据
public void deletePoem(Poem poem) {
long id = poem.getId();
database.delete(PoemDatabaseHelper.TABLE_NAME, PoemDatabaseHelper.COLUMN_ID + " = " + id, null);
}
// 更新一条数据
public void updatePoem(Poem poem) {
long id = poem.getId();
ContentValues values = new ContentValues();
values.put(PoemDatabaseHelper.COLUMN_TITLE, poem.getTitle());
values.put(PoemDatabaseHelper.COLUMN_AUTHOR, poem.getAuthor());
values.put(PoemDatabaseHelper.COLUMN_DYNASTY, poem.getDynasty());
values.put(PoemDatabaseHelper.COLUMN_CONTENT, poem.getContent());
database.update(PoemDatabaseHelper.TABLE_NAME, values, PoemDatabaseHelper.COLUMN_ID + " = " + id, null);
}
// 查询所有数据
public List<Poem> getAllPoems() {
List<Poem> poems = new ArrayList<>();
Cursor cursor = database.query(PoemDatabaseHelper.TABLE_NAME,
new String[] { PoemDatabaseHelper.COLUMN_ID, PoemDatabaseHelper.COLUMN_TITLE,
PoemDatabaseHelper.COLUMN_AUTHOR, PoemDatabaseHelper.COLUMN_DYNASTY,
PoemDatabaseHelper.COLUMN_CONTENT },
null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Poem poem = cursorToPoem(cursor);
poems.add(poem);
cursor.moveToNext();
}
cursor.close();
return poems;
}
// 将查询结果转换为 Poem 对象
private Poem cursorToPoem(Cursor cursor) {
long id = cursor.getLong(0);
String title = cursor.getString(1);
String author = cursor.getString(2);
String dynasty = cursor.getString(3);
String content = cursor.getString(4);
return new Poem(id, title, author, dynasty, content);
}
}
```
在这个类中,我们定义了插入、删除、更新和查询数据的方法。其中,插入数据的方法使用了 `ContentValues` 类来封装数据,查询数据的方法将查询结果转换为 `Poem` 对象。注意,在删除和更新数据时,我们需要通过 `id` 字段来定位到具体的数据。
最后,我们可以在 Activity 中使用这个数据库:
```java
public class MainActivity extends AppCompatActivity {
private PoemDataSource dataSource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataSource = new PoemDataSource(this);
dataSource.open();
// 插入一条数据
Poem newPoem = dataSource.insertPoem("静夜思", "李白", "唐代",
"床前明月光,疑是地上霜。\n举头望明月,低头思故乡。");
// 查询所有数据
List<Poem> poems = dataSource.getAllPoems();
// 更新一条数据
Poem poem = poems.get(0);
poem.setTitle("登高");
dataSource.updatePoem(poem);
// 删除一条数据
dataSource.deletePoem(poem);
dataSource.close();
}
}
```
在 Activity 中,我们先创建了一个 `PoemDataSource` 对象,并调用其 `open` 方法打开数据库。接着,我们可以插入一条数据、查询所有数据、更新一条数据和删除一条数据。最后,我们调用 `close` 方法关闭数据库。
至此,我们已经完成了一个简单的古诗笔记本应用的开发。当然,这只是一个简单示例,实际开发中还需要考虑很多细节问题,例如如何更好地呈现数据、如何处理用户输入等等。
阅读全文