android用sqllite数据库实现古诗的增、改、查,写一个完整的简单示例,点明文件名
时间: 2023-07-16 20:16:03 浏览: 145
好的,这里是一个简单的示例,演示了如何在 Android 应用程序中使用 SQLite 数据库来实现古诗的增、改、查。
首先,创建一个名为 "PoetryDbHelper.java" 的类,该类将承担与 SQLite 数据库的连接和数据读写的任务。
```java
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class PoetryDbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "PoetryDatabase.db";
public PoetryDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(PoetryContract.PoetryEntry.CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(PoetryContract.PoetryEntry.DELETE_TABLE);
onCreate(db);
}
}
```
接下来,创建一个名为 "PoetryContract.java" 的类,该类定义了数据库表的结构和字段。
```java
import android.provider.BaseColumns;
public final class PoetryContract {
private PoetryContract() {}
public static class PoetryEntry implements BaseColumns {
public static final String TABLE_NAME = "poetry";
public static final String COLUMN_TITLE = "title";
public static final String COLUMN_AUTHOR = "author";
public static final String COLUMN_CONTENT = "content";
public static final String CREATE_TABLE = "CREATE TABLE " +
TABLE_NAME + " (" +
_ID + " INTEGER PRIMARY KEY," +
COLUMN_TITLE + " TEXT," +
COLUMN_AUTHOR + " TEXT," +
COLUMN_CONTENT + " TEXT)";
public static final String DELETE_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
}
}
```
然后,在要使用数据库的 Activity 中,创建一个 PoetryDbHelper 对象,并使用它来打开数据库。接下来,您可以使用 SQLiteDatabase 对象执行查询、插入、更新和删除操作。
```java
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private PoetryDbHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new PoetryDbHelper(this);
// 添加新的古诗
addPoetry("静夜思", "李白", "床前明月光,疑是地上霜。举头望明月,低头思故乡。");
// 更新一首古诗
updatePoetry(1, "静夜思", "李白", "床前明月光,疑是地上霜。举头望明月,低头思故乡。\n窗前明月光,疑是地上霜。举头望明月,低头思故乡。");
// 查询所有的古诗
String allPoetry = getAllPoetry();
TextView textView = findViewById(R.id.text_view);
textView.setText(allPoetry);
}
private void addPoetry(String title, String author, String content) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(PoetryContract.PoetryEntry.COLUMN_TITLE, title);
values.put(PoetryContract.PoetryEntry.COLUMN_AUTHOR, author);
values.put(PoetryContract.PoetryEntry.COLUMN_CONTENT, content);
db.insert(PoetryContract.PoetryEntry.TABLE_NAME, null, values);
}
private void updatePoetry(int id, String title, String author, String content) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(PoetryContract.PoetryEntry.COLUMN_TITLE, title);
values.put(PoetryContract.PoetryEntry.COLUMN_AUTHOR, author);
values.put(PoetryContract.PoetryEntry.COLUMN_CONTENT, content);
String selection = PoetryContract.PoetryEntry._ID + " = ?";
String[] selectionArgs = { String.valueOf(id) };
db.update(PoetryContract.PoetryEntry.TABLE_NAME, values, selection, selectionArgs);
}
private String getAllPoetry() {
SQLiteDatabase db = dbHelper.getReadableDatabase();
String[] projection = {
PoetryContract.PoetryEntry._ID,
PoetryContract.PoetryEntry.COLUMN_TITLE,
PoetryContract.PoetryEntry.COLUMN_AUTHOR,
PoetryContract.PoetryEntry.COLUMN_CONTENT
};
Cursor cursor = db.query(
PoetryContract.PoetryEntry.TABLE_NAME,
projection,
null,
null,
null,
null,
null
);
StringBuilder builder = new StringBuilder();
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndexOrThrow(PoetryContract.PoetryEntry._ID));
String title = cursor.getString(cursor.getColumnIndexOrThrow(PoetryContract.PoetryEntry.COLUMN_TITLE));
String author = cursor.getString(cursor.getColumnIndexOrThrow(PoetryContract.PoetryEntry.COLUMN_AUTHOR));
String content = cursor.getString(cursor.getColumnIndexOrThrow(PoetryContract.PoetryEntry.COLUMN_CONTENT));
builder.append(id).append(". ").append(title).append(" - ").append(author).append("\n").append(content).append("\n\n");
}
cursor.close();
return builder.toString();
}
}
```
以上就是一个简单的使用 SQLite 数据库的示例。请注意,由于这只是一个演示,您需要自己创建布局文件等其他必要文件。
阅读全文