尝试创建SQLite数据库时,我收到以下LogCat异常:
引起:android.database.sqlite.SQLiteException:无法将只读数据库从版本0升级到1
这是我的代码:
import static android.provider.BaseColumns._ID;
import static org.inetizen.university_timetable.Constants.DAY_OF_WEEK;
import static org.inetizen.university_timetable.Constants.DURATION;
import static org.inetizen.university_timetable.Constants.MODULE_CODE;
import static org.inetizen.university_timetable.Constants.ROOM;
import static org.inetizen.university_timetable.Constants.START_TIME;
import static org.inetizen.university_timetable.Constants.TABLE_NAME;
import static org.inetizen.university_timetable.Constants.TYPE_OF_SESSION;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class TimeTableData extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "timetable.db";
private static final int DATABASE_VERSION = 1;
/** Create a helper object for the TimeTable database **/
public TimeTableData(Context ctx) {
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE …Run Code Online (Sandbox Code Playgroud) 我正在使用第一种方法开发基于Google IO演示架构的应用程序.基本上我有一个Service,ContentProvider由SQLiteDB 支持,我也使用Loaders.
当我的数据库发生更改时,我需要一种更新UI的方法.例如,用户可能想要将项目添加到他的购物篮中.将项目ID插入到篮子表后,我想更新UI.我应该使用什么方法?ContentObserver到目前为止,我看到的信息很少.这是要走的路吗?
android android-contentprovider android-loader android-sqlite
我搜索了很多,但是我没有找到关于如何在RecyclerView中显示SQLite数据的任何分步指南.任何人都可以向我解释我该怎么做?
我的DataBaseAdapter:
public class DataBaseAdapter {
DataBaseHelper helper;
public DataBaseAdapter(Context context) {
helper = new DataBaseHelper(context);
}
public long insertData(String name, String card, String code) {
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(DataBaseHelper.NAME, name);
contentValues.put(DataBaseHelper.CARD, card);
contentValues.put(DataBaseHelper.CODE, code);
long id = db.insert(DataBaseHelper.TABLE_NAME, null, contentValues);
return id;
}
public String getData(String name){
//select _id,Name,Card,Code
SQLiteDatabase db = helper.getWritableDatabase();
String[] columns = {DataBaseHelper.NAME, DataBaseHelper.CARD, DataBaseHelper.CODE};
Cursor cursor = db.query(DataBaseHelper.TABLE_NAME, columns,DataBaseHelper.NAME+" = '"+name+"'", null, null, null, null);
StringBuffer buffer …Run Code Online (Sandbox Code Playgroud) 我目前正在为我的应用程序中的ContentProvider编写测试用例(这是一个支票簿录制应用程序),并且在比较双值时我遇到了测试失败.说明这一点的最好方法是使用代码.我有这个函数返回要插入数据库的帐户对象的ContentValues:
private ContentValues getAccountContentValues(){
String testName = "Capital One";
double testStartingBalance = 3000.00;
ContentValues accountValues = new ContentValues();
accountValues.put(AccountEntry.COLUMN_NAME, testName);
accountValues.put(AccountEntry.COLUMN_BALANCE, testStartingBalance);
return accountValues;
}
Run Code Online (Sandbox Code Playgroud)
在函数内部,testInsertReadProvider()我有以下代码,以插入该帐户:
// Get Account values and write them
ContentValues accountValues = getAccountContentValues();
Uri accountInsertUri =
mContext.getContentResolver().insert(AccountEntry.CONTENT_URI, accountValues);
long accountRowId = ContentUris.parseId(accountInsertUri);
// Verify we got a row back
assertTrue(accountRowId > 0);
// A cursor is the primary interface to the query results
Cursor cursor = mContext.getContentResolver().query(
AccountEntry.CONTENT_URI, // Table name
null, // columns …Run Code Online (Sandbox Code Playgroud) 我正在尝试将功能合并到备份和恢复应用程序数据库到谷歌驱动器应用程序文件夹(用户不可见).我浏览了为android 提供的基本设置指南设置指南,可以让用户授权应用程序并到达调用onConnected方法的位置.
我面临的问题是,我不知道如何将数据库文件(.db)从设备"发送"到google drive app文件夹.Google已经共享了用于创建新文件的代码段,但就此而言.我确实发现了一个前面提到的问题,模糊地类似于我正在寻找的Google驱动器备份和恢复Android应用程序的数据库和共享首选项,但后来又不是我想要的.
搜索谷歌不会返回任何有用的链接,因为这是相对较新的API.
更新1:
共享onConnected()代码,
public void onConnected(Bundle bundle) {
Toast.makeText(GoogleSignIn.this, "In onConnected activity", Toast.LENGTH_SHORT).show();
// Testing to see if database is indeed uploaded to google drive app folder
String db_name = "XXX_DB";
String currentDBPath = "/data/" + "com.abc.efg" + "/databases/" + db_name;
saveToDrive(
Drive.DriveApi.getAppFolder(mGoogleApiClientDrive),
"XXX_DB.db",
"application/x-sqlite3",
new java.io.File(currentDBPath)
);
}
Run Code Online (Sandbox Code Playgroud)
更新2:
下面分享的解决方案非常适合将数据库上传到google drive app文件夹.对于那些可能面临类似问题的人,请尝试将数据库路径更改为"/data/data/com.abc.efg/databases/" + db_name而不是"/data/com.abc.efg/databases/" + db_name
下一步是能够从google drive app文件夹中检索和恢复数据库.如果我能够使它工作,应该更新这个问题.
我想创建一个更新触发器,
我可以在“BEGIN”和“END”之间创建一个变量,并通过 select sql 语句的查询设置值吗?然后通过判断这个变量来执行“if”“else”语句
请提示,如何在SQLite中使用REGEXP?
实现:
SELECT field FROM table WHERE field REGEXP '123'
Run Code Online (Sandbox Code Playgroud)
它不起作用. Error: no such function: REGEXP
我想使用 fts5 扩展创建一个表
使用 fts5 创建虚拟表 TABLE_FTS_FOOD_ITEM (content='food_items', Shrt_Desc, Energy_Kcal);
但我收到这个错误。
android.database.sqlite.SQLiteException:没有这样的模块:fts5
早些时候我使用的是 fts4,运行良好。我可以在 android 中将 fts5 与 sqLitedatabase 一起使用吗?
DB文件恢复后如何正确重新打开数据库?我在 AppModule 中打开它,如下所示:
@Singleton
@Provides
fun provideDb(app: Application): FastcountDb {
val db: FastcountDb = Room.databaseBuilder(app, FastcountDb::class.java, AppConfig.DB_NAME + ".db")
.fallbackToDestructiveMigration()
.setJournalMode(RoomDatabase.JournalMode.TRUNCATE)
.build()
return db
}
Run Code Online (Sandbox Code Playgroud)
当我备份或恢复数据库文件时,我只需调用 RoomDatabase.close() 复制数据库文件,然后需要重新打开数据库。有什么方法可以再次触发provideDb(app:Application)吗?
我想设置我自己的冲突列而不是默认主键(在我的情况下只是一个自动增量整数)。所以我的表是:
@Entity(tableName = "users_tab")
data class User(
@PrimaryKey(autoGenerate = true)
var id: Int,
var str1: String,
var str2: String,
var str3: String
)
@Dao
interface UserDAO {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(users: List<User>)
}
Run Code Online (Sandbox Code Playgroud)
我想什么REPLACE时候User有相同的str1以及str2我应该如何修改我的Insert注释?
android-sqlite ×10
android ×8
android-room ×2
sqlite ×2
datatrigger ×1
java ×1
kotlin ×1
regex ×1
testing ×1