android - 我想将 sqlite 数据库中的数据检索到另一个活动的 textview 中,我的数据库代码如下所示
问题描述
try {
String qu = "CREATE TABLE IF NOT EXISTS ABOUTINST(body varchar(10000));";
database.execSQL(qu);
} catch (Exception e) {
Toast.makeText(activity, "Error Occured for create table", Toast.LENGTH_LONG).show();
}
解决方案
已编辑
使用此 SQLiteOpenHelper 管理您的特定数据库
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class UserDbHelper extends SQLiteOpenHelper {
// replace constant for your database name
private static final String DATABASE_NAME = “YOUR_DB_NAME”;
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = “abount_inst”;
private static final String CREATE_QUERY = "CREATE TABLE IF NOT EXISTS abount_inst(id INTEGER PRIMARY KEY AUTOINCREMENT, body varchar(10000))";
public UserDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.e("DATABASE OPERATION", "Database created / opened.....");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_QUERY);
Log.e("DATABASE OPERATION", "Table create..." + CREATE_QUERY);
}
// method to retrieve body using PRIMARY_KEY of the table i.e. id
public String getBody(int id, SQLiteDatabase db) {
Cursor cursor = null;
String body = "";
try {
String selectQuery = "SELECT * FROM " + TABLE_NAME + " WHERE id = " + id;
cursor = db.rawQuery(selectQuery, null);
if (cursor.getCount() > 0) {
cursor.moveToFirst();
body = cursor.getString(cursor.getColumnIndex("body"));
}
} finally {
cursor.close();
return body;
}
}
}
推荐阅读
- sql - MS-Access-2016 查询语言“ORDER BY”子句
- javascript - 带有动态 HTML 页面的锚点
- angular - 在角度中使用参数作为对象属性
- clockify - 身份证可靠吗?
- javascript - 如何使用 JavaScript 接收自定义视觉模型的判断结果
- javascript - 有没有办法离线保存大量图像以便稍后发送到服务器?
- javascript - 如何检查数组值是否具有文件格式
- javascript - 使用文件保护程序 npm 包下载时 xlsx 文件损坏
- swift - Swift macOS 标签渐变文本
- javascript - 有什么方法可以在 Chrome DevTools 中重新分配 const 吗?