我已经构建了一个应用程序,现在我想使用我的新备份应用程序复制正在运行的应用程序的数据库.我通过DB_PATH + DB_NAME使用以下值创建数据库路径:
DB_PATH = "/data/data/iCam.Cam/";
DB_NAME = "testdb.db";
Run Code Online (Sandbox Code Playgroud)
我有代码将数据库从给定路径复制到SD卡.但是,当我最初使用以下方法检查数据库时,它返回false:
public boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
e.fillInStackTrace();
// database does't exist yet.
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
Run Code Online (Sandbox Code Playgroud)
有关如何实现这一目标的任何建议?
CSV数据库与Android中的SQLite数据库相比如何?
查看StackOverflow上的其他问题,并阅读Android Developer Documentation,我看到SQLite数据库的使用频率远高于从CSV文件中读取数据.还有一些问题,用户希望将CSV文件导入SQLite数据库(例如,此问题或此问题).使用SQLite而不是CSV有优势吗?
我尝试过使用CSV和SQLite一点点,在性能方面我没有看到很大的区别,但如果我在这里错了,请纠正我.
据我所知,有不同的读取CSV文件的方法,我打开并使用BufferedReader类似的方式读取它:
BufferedReader reader =
new BufferedReader(new InputStreamReader(context.getAssets().open(fileName)));
Run Code Online (Sandbox Code Playgroud)
SQLite数据库以通常的方式打开:
SQLiteDatabase db = helper.getReadableDatabase();
Run Code Online (Sandbox Code Playgroud)
我不太确定功能上的差异,虽然我假设SQLite更容易管理和过滤,这就是我问这个问题的原因.
总结一下:
SQLiteDatabase类?为了澄清,我知道CSV文件只是一个用逗号分隔值的文件,即不是作为SQL数据库的"数据库".但是,我仍然可以将CSV文件用作一种数据库,其中逗号分隔值表示不同的列,也可以通过检查特定列是否与特定值匹配来过滤.所以我问哪个更好从中读取数据.
这是我的第一个ROOM实现。我有一个User类,其中有一个Pets列表作为其成员变量之一。
public class User {
String id;
String name;
List<Pet> pets;
}
Public class Pets {
String id;
String type;
}
Run Code Online (Sandbox Code Playgroud)
如何通过以下JSON为Room Database创建其Entity和DAO类?
{
"users":[
{
"id":"as123",
"name":"John",
"pets":[
{
"id":"p123",
"type":"dog"
}
]
},
{
"id":"as343",
"name":"Mark",
"pets":[
{
"id":"p324",
"type":"dog"
},
{
"id":"p254",
"type":"cat"
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
我创建了以下内容,但不确定如何创建正确的列或联接这些表。
实体类别
@Entity(tableName = "user")
public class User {
@PrimaryKey
@NonNull
@ColumnInfo(name = "id")
String id;
@Nullable
@ColumnInfo(name = "name")
String name;
List<Pets> pets; // …Run Code Online (Sandbox Code Playgroud) sqlite android android-database android-room android-architecture-components
我只在android 9中遇到异常,重新安装一切看起来不错之后,
例外:
android.database.sqlite.SQLiteBlobTooBigException: Row too big to fit into CursorWindow requiredPos=0, totalRows=1...
Run Code Online (Sandbox Code Playgroud)
码:
Cursor cursor = database.query(......);
if(cursor == null || cursor.getCount() < 0) { //Here is the error
Log.d("Error", "count : null");
return "";
}
Run Code Online (Sandbox Code Playgroud)
编辑:
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:354)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Caused by: android.database.sqlite.SQLiteBlobTooBigException: Row too big to fit into CursorWindow requiredPos=0, totalRows=1
at android.database.sqlite.SQLiteConnection.nativeExecuteForCursorWindow(Native Method)
at android.database.sqlite.SQLiteConnection.executeForCursorWindow(SQLiteConnection.java:859)
at android.database.sqlite.SQLiteSession.executeForCursorWindow(SQLiteSession.java:836)
at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62) …Run Code Online (Sandbox Code Playgroud) 直到Android P一切都运行良好我的代码,但现在在Android P我在使用数据库时遇到了很多问题.
我有.db sqlite数据库文件存储在assets文件夹中.我正在将它们导入android的数据库空间,它工作正常,直到Android P.在android P中我得到了这个异常:SQLite: No Such Table Error
我在这里搜索并找到了这个,我试着应用接受的答案: Android P - 'SQLite:没有这样的表错误'从资产复制数据库后
问题是,现在我遇到了另一个问题,但仍然无效.现在我遇到的问题是,每当我尝试检查数据库是否已存在时,此代码始终返回true,因此我从不创建数据库.即使最近安装了应用程序,即使未创建数据库,也是如此:
private boolean checkIfDBExists() {
File dbFile = new File(DB_COMPLETE_PATH);
return dbFile.exists();
}
Run Code Online (Sandbox Code Playgroud)
我的源代码是之前引用的stackoverflow问题中接受的答案的源代码.
新的Android P需要获取数据库路径的方法:
public static String getDatabasePath(String fileNname){
MySQLiteOpenHelper helper = new MySQLiteOpenHelper(ApplicationContextProvider.getContext(), fileNname);
SQLiteDatabase database = helper.getReadableDatabase();
String path = database.getPath();
database.close();
return path;
}
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
public MySQLiteOpenHelper(Context context, String databaseName) {
super(context, databaseName, null, 2);
}
@Override
public void onCreate(SQLiteDatabase db) …Run Code Online (Sandbox Code Playgroud) sqlite android android-sqlite android-database android-9.0-pie
我正在尝试将音频/视频转换为字节数组,反之亦然,使用下面的代码可以将音频/视频文件转换为字节数组(见下面的代码),但无法转换大文件(超过 50MB 的文件)是否有任何限制。 ? 如何将字节数组转换为音频/视频文件。?请帮帮我。
public byte[] convert(String path) throws IOException {
FileInputStream fis = new FileInputStream(path);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
for (int readNum; (readNum = fis.read(b)) != -1;) {
bos.write(b, 0, readNum);
}
byte[] bytes = bos.toByteArray();
return bytes;
}
Run Code Online (Sandbox Code Playgroud)
请帮忙得到结果
在我的Android项目(Kotlin)中,我想为我的DATA LAYER使用Room持久性库.
但是当我为Room Persistent库添加依赖项时,突然构建项目开始失败.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext {
globalCompileSdkVersion = 26
globalMinSdkVersion = 19
globalTargetSdkVersion = 26
kotlin_version = '1.2.10'
support_version = "27.0.2"
constraint_layout_version = "1.0.2"
view_animator_version = "1.0.5"
junit_version = "4.12"
runner_version = "1.0.1"
espresso_core_version = "3.0.1"
room_version = "1.0.0"
dagger_version = "2.13"
rxJavaVersion = "2.1.7"
rxAndroidVersion = "2.0.1"
libs = [
appCompatV7 : "com.android.support:appcompat-v7:$support_version",
rxJava : "io.reactivex.rxjava2:rxjava:$rxJavaVersion",
rxAndroid …Run Code Online (Sandbox Code Playgroud) 我正在尝试将房间数据库集成到我的 android 应用程序中。现在我想从数据库查询不同的结果,但我收到此错误:
error: The columns returned by the query does not have the fields [id] in com.abc.def.model.User even though they are annotated as non-null or primitive.
Columns returned by the query: [user_name]
Run Code Online (Sandbox Code Playgroud)
我的实体(Getter 和 Setter 不在此处复制):
@Entity
public class User {
@PrimaryKey(autoGenerate = true)
@NonNull
@ColumnInfo(name = "id")
private Integer id;
@ColumnInfo(name = "user_name")
@NonNull
private String name;
@ColumnInfo(name = "email")
private String email;
public User(String name, String email) {
this.name = name;
this.email = email;
}
}
Run Code Online (Sandbox Code Playgroud)
我的道: …
@Dao
interface ExampleDao {
@Query("SELECT * FROM example_table WHERE id = :id")
fun get(id: Int): LiveData<Example>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(something: Example)
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用下面的行检查数据库中是否存在某一行时
val exists = dao.get(id).value != null
if (exists) {
...
}
Run Code Online (Sandbox Code Playgroud)
存在变量总是返回 null我知道数据库中已经有该行并且 UI 正确显示了信息。为什么它总是返回 null,我如何检查房间数据库中是否有该行?
我的实体类:
@Entity(tableName = "student")
data class Student(
var name: String,
var age: Int,
var gpa: Double,
var isSingle: Boolean,
@PrimaryKey(autoGenerate = true)
var id: Long = 0,
@Ignore //don't create column in database, just for run time use
var isSelected: Boolean = false
)
Run Code Online (Sandbox Code Playgroud)
然后我像这样插入(在 中测试androidTest):
val student = Student("Sam", 27, 3.5, true)
studentDao.insert(student)
Run Code Online (Sandbox Code Playgroud)
添加@Ignore注释后,它立即给了我这个错误:
C:\Android Project\RoomTest\app\build\tmp\kapt3\stubs\debug\com\example\roomtest\database\Student.java:7: ????: Entities and POJOs must have a usable public constructor. You can have an empty constructor or a constructor …Run Code Online (Sandbox Code Playgroud) android ×10
android-database ×10
android-room ×5
sqlite ×3
kotlin ×2
android-architecture-components ×1
csv ×1
java ×1