我想使用ContentResolver.update(Uri uri, ContentValues values, String where, String[] selectionArgs)
我知道如何使用它的方法,但我想知道它对 ContentValues 做了什么。它是覆盖所有现有的 ContentValues,还是只覆盖给定的 ContentValues?
例如,这些 ContentValues 存在:
A: abc
B: 123
C: 456
Run Code Online (Sandbox Code Playgroud)
update()方法中的 ContentValues包含以下内容:
A: asdf
C: 789
Run Code Online (Sandbox Code Playgroud)
那么新的 ContentValues 将是:(仅覆盖给定的值)
A: asdf
B: 123
C: 789
Run Code Online (Sandbox Code Playgroud)
或者会是:(覆盖所有值)
A: asdf
C: 789
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
contentResolver.update(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values, "_data=" + audioFilePath, null);
Run Code Online (Sandbox Code Playgroud)
其中values包含ContentValues应该被覆盖的 并且audioFilePath包含音频文件的路径(这是 的值_data)。
03-02 04:23:07.015:E/StrictMode(1096):在附加的堆栈跟踪中获取了资源,但从未释放。有关避免资源泄漏的信息,请参阅 java.io.Closeable。03-02 04:23:07.015: E/StrictMode(1096): java.lang.Throwable: 未调用显式终止方法“close” 03-02 04:23:07.015: E/StrictMode(1096): 在 dalvik.system .CloseGuard.open(CloseGuard.java:184) 03-02 04:23:07.015: E/StrictMode(1096): at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:805) 03-02 04:23 :07.015: E/StrictMode(1096): 在 android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789) 03-02 04:23:07.015: E/StrictMode(1096): 在 android.database.sqlite。 SQLiteDatabase.openDatabase(SQLiteDatabase.java:694) 03-02 04:23:07.015: E/StrictMode(1096): 在 android.app.ContextImpl。
我在“谷歌搜索”这个时遇到了麻烦,但希望它对专家(甚至中级)来说不是太棘手。
有没有办法在未知数量的多个 OR 条件中搜索子字符串?
Android SQLite Room Persistence DAO 的原始查询是:
SELECT country FROM table WHERE country IN (:searchList)
Run Code Online (Sandbox Code Playgroud)
其中,给定 ['Paris, France', 'Berlin, Germany'] 的 searchList 将转换为 SQL:
SELECT country FROM table WHERE country IN ( 'Paris, France', 'Berlin, Germany' );
Run Code Online (Sandbox Code Playgroud)
并且会返回:['巴黎,法国','柏林,德国']
但是,我希望能够仅按国家/地区进行搜索。例如,给定 ['France', 'Germany'] 的搜索列表,不幸的是我没有得到任何结果。
我知道以下 SQL 可以解决问题:
SELECT country FROM table WHERE country LIKE '%France%' OR country LIKE '%Germany%';
Run Code Online (Sandbox Code Playgroud)
但是,因为我不知道 searchList 中有多少元素,所以我必须使用原始格式:
SELECT country FROM table WHERE country IN (:searchList)
Run Code Online (Sandbox Code Playgroud)
有什么方法可以使上述搜索国家/地区列中的子字符串?
非常感谢您的帮助,
担
假设我们有以下实体:项目:
class Item {
...
@Index(unique=true)
private String guid;
...
@ToMany
@JoinEntity(entity = JoinItemsWithTags.class, sourceProperty = "itemGuid", targetProperty = "tagName")
private List<Tag> tagsWithThisItem;
...
}
Run Code Online (Sandbox Code Playgroud)
标签:
class Tag {
@Id
private Long localId;
@Index(unique = true)
private String name;
...
}
Run Code Online (Sandbox Code Playgroud)
我们需要加入他们.这是我的连接实体类:
@Entity(nameInDb = "item_tag_relations")
class JoinItemsWithTags {
@Id
private Long id;
private String itemGuid;
private String tagName;
...
}
Run Code Online (Sandbox Code Playgroud)
我想使用标记名称作为连接属性而不是Long id,因为在与服务器同步时更容易支持一致性.但是当前Item类中的标签getter总是返回一个空列表.我查看了日志并找到了在getter中内部使用的生成查询:
SELECT * <<-- there were a long sequence of fields
FROM "tags" T JOIN item_tag_relations J1
ON T."_id"=J1."TAG_NAME" …Run Code Online (Sandbox Code Playgroud) 我有一个实体Memo类。我@TypeConverter用来转换GregorianCalendar为Long.
备忘录.java
@Entity
public class Memo {
@Ignore
public static final int TYPE_EXPENSE = 0;
@Ignore
public static final int TYPE_INCOME = 1;
@PrimaryKey(autoGenerate = true)
public int id;
public int type;
@ColumnInfo(name = "item_name")
public String itemName;
@ColumnInfo(name = "category_name")
public String categoryName;
public String note;
public double dollar;
public GregorianCalendar date;
public String photoPath;
@Ignore
public Memo(int type) {
this.type = type;
itemName = "";
categoryName = "";
note = …Run Code Online (Sandbox Code Playgroud) 我对访问SQLiteDatabase. 它应该是一个连接或多个连接才能从多个线程访问。我已经阅读了很多文章,包括以下两篇。
/sf/answers/258291841/ http://touchlabblog.tumblr.com/post/24474398246/android-sqlite-locking
这两者都建议使用单一连接。甚至我自己对同一问题的回答也被 OP 接受了。我使用了Singleton访问SQLiteopenHelper类的方法。
但是在阅读了enableWriteAheadLogging哪些州的文档后我仍然感到困惑
此方法支持从同一数据库上的多个线程并行执行查询。它通过打开到数据库的多个连接并为每个查询使用不同的数据库连接来实现这一点。数据库日志模式也更改为允许写入与读取同时进行。
现在这是令人困惑的部分。如果我想从多个线程同时访问数据库,我可以Singleton访问SQLiteOpenHelper它,在我的理解中这意味着插入的串行执行,同时同步读取可以无错误地完成。但是上面的文档说,为了同时访问,enableWriteAheadLogging应该调用它返回创建多个连接。这是怎么回事??如果我getWritableDatabase()通过Singleton SQLiteOpenHelper从多个线程调用using来进行插入,这意味着什么?电话会是连续的吗?应该enableWriteAheadLogging叫?
请说清楚。
sqlite multithreading android sqliteopenhelper android-sqlite
我在我的应用程序中使用 RoomDB。能够执行 crud 操作。其实我想看看db文件。
getDatabasePath("user.db").getAbsolutePath();
Run Code Online (Sandbox Code Playgroud)
上面的代码给了我db文件所在的目录,保存目录是这样的
/data/data/com.example.manvish.roomdb/databases/user.db
Run Code Online (Sandbox Code Playgroud)
但即使在命令提示符下使用 sudo ,我仍然无法访问数据目录。现在我想将 DB 文件位置更改为内部存储器或 SD 卡中的其他一些文件夹。我怎样才能做到这一点?
以下代码不会将自动增量 ID 重置/清除为 0
database = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, DatabaseMeta.DB_NAME)
.build();
database.query("DELETE FROM sqlite_sequence WHERE name = ?", new Object[]{"tableName"});
Run Code Online (Sandbox Code Playgroud)
或者
database.query("REPLACE INTO sqlite_sequence (name, seq) VALUES ('mission', -1)", null);
Run Code Online (Sandbox Code Playgroud)
无论如何要清除sqlite_sequenceAndroid房间吗?
我认为,Android 房间没有使用“sqlite_sequence”进行自动递增逻辑,我不确定。尝试打印sqlite_sequence条目,找不到我使用的表名(具有自动递增主键)。
我正在使用 Room 来保存数据。
我有一个实体,它有一个模拟票证系统的自动生成(自动生成)主键。
在每个应用程序运行时,我需要这个键从 0 开始。
实体:
@Entity
public class SequenceAction {
@PrimaryKey(autoGenerate = true)
private Integer sequenceId;
private String actionType;
private String extraInfo;
//getters & setters
}
Run Code Online (Sandbox Code Playgroud)
初始化:
// init sequenceAction object
// run with executor(sequenceId is automatically set on insert to table):
AppDatabase.getInstance(getContext()).sequenceActionDao().save(sequenceAction);
Run Code Online (Sandbox Code Playgroud)
我尝试过的事情:
我AppDatabase.getInstance(getApplicationContext()).clearAllTables(); 过去常常在退出时清除表,但这不会重置键起始索引,而是从上次运行时停止的位置开始。
我还没有找到使用 Room 执行此操作的方法,因此我正在尝试将 SimpleSQLiteQuery 传递给 Dao 中的 RawQuery 方法:
//Dao
@RawQuery()
Integer init(SimpleSQLiteQuery query);
//Passed query
new SimpleSQLiteQuery("...query...");
Run Code Online (Sandbox Code Playgroud)
我试过下一个查询:
"ALTER TABLE SequenceAction AUTO_INCREMENT = 0"我收到一个错误(我用“AUTOINCREMENT”试过这个,同样的错误):
android.database.sqlite.SQLiteException:在“AUTO_INCREMENT”附近:语法错误(代码 …
我有一种情况,我想使用 Room 持久性库将数据插入 Master (store_master) 和 Mapping (store_mapping) 表中的一对多关系。我的实现如下:
@Entity(tableName = "store_master")
class Store {
@PrimaryKey(autoGenerate = true)
public var store_id: Int = 0
@SerializedName("name")
lateinit var store_name: String
}
@Entity(tableName = "store_mapping", foreignKeys = arrayOf(
ForeignKey(entity = Store::class,
parentColumns = arrayOf("store_id"),
childColumns = arrayOf("pic_id"),
onDelete = CASCADE)))
class StorePicture(@field:PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id") var id: Int,
@SerializedName("pic_id") var pic_id: Int?,
@SerializedName("image") var storage_picture: String?)
class StoreWithPictures {
@Embedded
var store: Store? = null
@Relation(parentColumn = "store_id",
entityColumn = …Run Code Online (Sandbox Code Playgroud) android android-sqlite android-room android-architecture-components android-jetpack
android ×10
android-sqlite ×10
android-room ×6
sqlite ×4
android-architecture-components ×1
greendao ×1
greendao3 ×1