我需要存储我的Android应用程序内容,并需要有关我应该使用的存储空间的提示.SQLite数据库或XML文件?
应用程序和数据的一些特征:
我想在XML文件中存储具有这些要求/特征的数据没有性能问题.如果是这样,我应该将每个测量类型分开在单独的XML文件中以及移动设备上应该存储文件的位置吗?
你可能记得我之前关于Green DAO双向关系的问题,我的聊天内容包括对话和消息.
每条消息都有一个父对话,对话中有一条消息列表.
当我尝试添加新消息时,只要我不关闭应用程序,它就能完美运行.
这是我用于向现有对话添加消息的代码:
我现在有我的代码执行此操作:
Conversation conv = getConversation();
List<Message> list = conv.getMessageList();
Message msg = new Message();
msg.setConversationId(conv,getId());
MessageDao.insert(msg);
list.add(msg);
conv.update();
// ConversationDao.update(conv);
Run Code Online (Sandbox Code Playgroud)
当我下次打开应用程序并调用时conv.getMessageList(),结果为空ArrayList.
我究竟做错了什么?
编辑:
我改变了我的代码,现在它看起来像这样:
Conversation conv= getCOnversation();
Message msg = new Message();
msg.setConversation(conv);
MessageDao.insert(msg);
conv.resetMessageList();
Run Code Online (Sandbox Code Playgroud)
不幸的是,现在比以往任何时候都更多(即使在同一次运行中),仍然调用conv.getMessageList()返回空java.list.
编辑2:
这里是生成器的代码(这不是真正的代码,而只是对问题很重要的代码.
Entity message = schema.addEntity("Message");
message.addIdProperty().autoincrement();
Entity conversation =schema.addEntity("Conversation");
conversation.addIdProperty().autoincrement();
Property parentConversation = message.addLongProperty("parentConversation")
.getProperty();
Property messages = conversation.addLongProperty("messages").getProperty();
message.addToOne(conversation, parentConversation);
conversation.addToMany(message, messages);
Run Code Online (Sandbox Code Playgroud)
编辑3
现在,而不是调用conv.getMessageList(); …
我对使用Sqlite数据库编程的Android非常陌生.我想通过desc我的游戏分数列来订购.游戏得分列类型是真实的,当我尝试通过gpoint和desc无法正常工作时.请给我一些建议.代码如下:
public List<Score> getAllScores() {
List<Score> scoreList = new ArrayList<Score>();
String selectQuery = "SELECT * FROM " + TABLE_SCORES+" ORDER BY gpoint DESC";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Score score = new Score();
score.setID(Integer.parseInt(cursor.getString(0)));
score.setPoint(cursor.getFloat(1));
score.setLetter(cursor.getString(3));
score.setDate(cursor.getString(2));
scoreList.add(0, score);
} while (cursor.moveToNext());
}
return scoreList;
}
Run Code Online (Sandbox Code Playgroud)
我的测试插入是:
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(c.getTime());
Log.d("Insert: ", "Inserting ..");
db.addScoreToDB(new Score((float)9.4, "S",formattedDate));
db.addScoreToDB(new Score((float)7.5, "S",formattedDate));
db.addScoreToDB(new …Run Code Online (Sandbox Code Playgroud) 我有三个表,并希望使用内容提供商来管理它们.下面是我的内容提供商提供的代码:
private static final UriMatcher sURIMatcher = new UriMatcher(
UriMatcher.NO_MATCH);
static {
sURIMatcher.addURI(AUTHORITY, METER_PATH, all_meters);
sURIMatcher.addURI(AUTHORITY, METER_PATH + "/#", single_meter);
sURIMatcher.addURI(AUTHORITY, CUSTOMERS_PATH, all_customers);
sURIMatcher.addURI(AUTHORITY, CUSTOMERS_PATH + "/#", single_customer);
sURIMatcher.addURI(AUTHORITY, BILL_PATH, all_bills);
sURIMatcher.addURI(AUTHORITY, BILL_PATH + "/#", single_bill);
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = database.getWritableDatabase();
// Using SQLiteQueryBuilder instead of query() method
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder
.setTables(MeterTableDetails.TABLE_METERS
+ " as meters "
+ " INNER JOIN "
+ …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个Web应用程序以及移动应用程序(使用PhoneGap).我正在使用HTML5/CSS/JS,因为我必须与PhoneGap兼容.我需要我的移动应用程序可用并离线工作(我应该能够至少将数据推送到数据库,不太关心检索它,可以限制用户上线获取数据).我知道它有点奇怪,但这就是我的要求.
我以某种方式降落到PouchDB上.在编写PhoneGap应用程序时,它似乎可以像我一样使用SQLite插件.正如这里所讨论的:http://pouchdb.com/adapters.html(Cordova/PhoneGap的SQLite插件) - 我知道它可以无缝脱机工作并在线时与PhouchDB同步,而无需任何开发人员干预.
我不想再浪费时间,只是盲目地使用PouchDB.我想在探索它之前确认它的功能.
注意:我的数据大部分都是普通文本,每次写入数据库时都会有一个图像.(我需要存储图片 - 在某个地方,也应该很容易在线和离线 - 欢迎任何建议在这里欢迎)
感谢和问候,
inblueswithu
当我们在设备上的开发过程中运行应用程序时,sharedPreferences重新安装时是否清除了首选项?
sqlite android sharedpreferences android-install-apk android-sqlite
我有一张简单的桌子.我正在尝试将默认值放入TEXT列.这是表查询:
"CREATE TABLE book(_id INTEGER PRIMARY KEY AUTOINCREMENT, book_id TEXT NOT NULL DEFAULT '0', book_name TEXT NOT NULL);"
Run Code Online (Sandbox Code Playgroud)
它创建表但我尝试插入数据时出现问题.我只是尝试给book_name一个书名,因为我预计book_id的列默认值为0.但它没有,它添加了空值,因此行不会插入.我也试过这个查询:
"CREATE TABLE book(_id INTEGER PRIMARY KEY AUTOINCREMENT, book_id TEXT NOT NULL DEFAULT \'0\', book_name TEXT NOT NULL);"
Run Code Online (Sandbox Code Playgroud)
但问题仍然存在.我已经搜索了堆栈溢出,得到了一些答案,但它们已经老了,现在还不适合我.因此,如何在sqlite中将默认值设置为TEXT列有所改变.提前致谢 :)
编辑
这是插入声明:
database.insert("book", null, cv);
Run Code Online (Sandbox Code Playgroud)
这里cv是ContentValues的对象,它只包含列book_name的值.
有没有办法将我的sqlite查询打印到logCat?这是我的查询
db.query(TABLE, projection, selection, selectionArgs, null, null, null)
Run Code Online (Sandbox Code Playgroud) 我在我的应用程序中使用Sugar ORM 1.5,我希望在用户注销时清除所有信息.
我想避免删除数据库文件或尽可能使用纯SQLite脚本.
我找到的唯一解决方案是在所有表上使用deleteAll,但我想知道是否有更好的方法来执行它.
问候,
编辑
我解决了在删除数据库SugarContext.terminate();之前删除数据库之前删除数据库的问题SugarContext.init(context);.
看起来这是Henry Dang在评论中指出的最佳解决方案,它比删除所有数据更快.
我试图获取包含特定字符串但查询不起作用的数据,以下是获取数据的方法.
public Cursor getSearch(String item) {
SQLiteDatabase db = this.getReadableDatabase();
String mQuery = "SELECT * FROM hadiths WHERE text LIKE %"+item.toString()+"%";
Cursor cursor = db.rawQuery(mQuery, null);
return cursor;
}
Run Code Online (Sandbox Code Playgroud)
Logcat显示以下错误.
引起:android.database.sqlite.SQLiteException:接近"%":语法错误(代码1):,同时编译:SELECT*FROM hadiths WHERE text LIKE%fast%
我知道通配符%%和字符串变量项导致问题,但如何使用字符串变量和通配符?