我想知道哪种方法是访问我的应用程序数据库的最佳方法:使用内容提供程序,或手动实现我的DAO?从我最近的调查来看,似乎内容提供商,即使是应用程序内部使用,也是可取的,但我不确切知道每种方法的缺点是什么.你能给出一些反馈意见吗?
我最近更新了我的一个(开源)Android应用程序,我的用户正在获得一个我无法复制的例外.关键部分是:
android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5)
然后
Caused by: android.database.sqlite.SQLiteException: Failed to change locale for db '/data/data/com.airlocksoftware.hackernews/databases/hacker_news_cache.db' to 'en_US'.
这种情况发生在使用Android 2.3 - 4.2.1的设备上,并且在我尝试连接数据库的应用程序中的多个位置.我使用它后关闭数据库.
我找不到有关"未能更改db语言环境"异常的更多信息.当我查看SQLiteConnection的源代码(第386行)时,它似乎是'android_metadata'表或'使用新的语言环境更新索引'的问题.
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.support.v4.content.ModernAsyncTask$3.done(ModernAsyncTask.java:137)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856) Caused by: android.database.sqlite.SQLiteException: Failed to change locale for db '/data/data/com.airlocksoftware.hackernews/databases/hacker_news_cache.db' to 'en_US'.
at android.database.sqlite.SQLiteConnection.setLocaleFromConfiguration(SQLiteConnection.java:386)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:218)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
at …Run Code Online (Sandbox Code Playgroud) 我有表A,其中包含以下值:
+------+------+ | ID1 | ID2 | +------+------+ | 1689 | 1709 | | 1709 | 1689 | | 1782 | 1709 | | 1911 | 1247 | | 1247 | 1468 | | 1641 | 1468 | | 1316 | 1304 | | 1501 | 1934 | | 1934 | 1501 | | 1025 | 1101 | +------+------+
和另一种关系(表B)具有以下值:
+------+------+ | ID1 | ID2 | +------+------+ | 1641 | 1468 | | 1911 | 1247 | +------+------+
我想删除表B中出现的表A中的所有行(与ID1和ID2完全匹配).理论上似乎很简单,但我对EXISTS声明或其他方法并不满意.我正在使用SQLite.
任何建议都非常感谢.
我正在尝试在sqlite中创建一个表,该表从csv文件中获取数据并向第一列添加自动增量主键.这是我试图将数据插入的表:
DROP TABLE IF EXISTS Allegiance;
CREATE TABLE Allegiance (
AllegianceID INTEGER PRIMARY KEY AUTOINCREMENT,
CharacterID INTEGER,
Title TEXT,
FOREIGN KEY (CharacterID) REFERENCES Characters(CharacterID));
Run Code Online (Sandbox Code Playgroud)
这是.csv文件中的数据
, 3, King of the North
, 14, King of the Andals and the First Men
, 15, Lord of Dragonstone
, 26, Khaleesi
, 35, Lord Reaper of Pyke
Run Code Online (Sandbox Code Playgroud)
这是我收到的错误:
sqlite> .mode csv
sqlite> import allegiances.csv Allegiance;
Error: datatype mismatch
Run Code Online (Sandbox Code Playgroud)
如果我在每行中的第一个逗号之前有"null",则会收到相同的错误.当我在每行的第一个逗号之前添加随机数时,我没有收到任何错误.但是,我需要使用的实际数据集可能要大得多,因此,我不能简单地为每个条目手动添加唯一的主键.我真的很感激这方面的一些帮助
我需要定期使用文件中收到的数据增加列中的值.该表有> 400000行.到目前为止,我的所有尝试都会导致性能非常差.我写了一个反映我要求的实验:
#create table
engine = create_engine('sqlite:///bulk_update.db', echo=False)
metadata = MetaData()
sometable = Table('sometable', metadata,
Column('id', Integer, Sequence('sometable_id_seq'), primary_key=True),
Column('column1', Integer),
Column('column2', Integer),
)
sometable.create(engine, checkfirst=True)
#initial population
conn = engine.connect()
nr_of_rows = 50000
insert_data = [ { 'column1': i, 'column2' : 0 } for i in range(1, nr_of_rows)]
result = conn.execute(sometable.insert(), insert_data)
#update
update_data = [ {'col1' : i, '_increment': randint(1, 500)} for i in range(1, nr_of_rows)]
print "nr_of_rows", nr_of_rows
print "start time : " + str(datetime.time(datetime.now()))
stmt = …Run Code Online (Sandbox Code Playgroud) 我有一个实体 - User.它由描述User.class.
Hibernate为每个实体创建一个表,所以当我调用时session.save(user),我的数据总是保存到该表中.
现在我需要另一个表来表示相同User类型的数据,我需要将我的实体保存到该表中.
数据结构(类似这样):
table users_1_table{
string id;
string username;
}
table users_2_table{
string id;
string username;
}
Run Code Online (Sandbox Code Playgroud)
使用这个:
session.save(user1,"users_1_table")
session.save(user2,"users_2_table")
Run Code Online (Sandbox Code Playgroud)
和结果,我应该user1在users_1_table和user2 中users_2_table.
由于系统限制,我不能将这两个对象放在一个表中.(即使创建额外的字段也是个坏主意).
我可以在没有子类化的情况下这样做吗?使用programmaticaly hibernate配置?
我正在开发一个Android应用程序,我正在创建一个名为HealthDev.db的数据库,它有一个名为rawData的表,它有4列:_id,foreignUserId,data,timeStamp
我已经使用bash shell中的程序sqlite3并且已经发现我可以使用以下列模式参数的时间戳列:timeStamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
所以当我创建表时我用过:create table rawData(_id integer primary key autoincrement,foreignUserId integer,data real,timeStamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
这在bash中运行良好.
然后我在sqlite3中练习并且知道当插入timeStamp列并使用函数time('now')作为存储它的值时,它实际上以通用协调时间的形式存储HH:MM:SS形式的时间戳.
所以现在将其转换为java for android app,我使用下面的代码.这样,当调用onCreate时,表会自动生成大约20行.这只是为了测试我是否正确地在java中传递时间('now').
// Below are variables to the database table name and the
// database column names.
public static final String TABLE_RAW_DATA = "rawData";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_FOREIGN_USER_ID = "foreignUserId";
public static final String COLUMN_DATA = "data";
public static final String COLUMN_TIME_STAMP = "timeStamp";
// Database creation sql statement.
private …Run Code Online (Sandbox Code Playgroud) 我在SQLite DB中存储Calendar.getTimeInMilliseconds()中的日期.我需要在SELECT语句中按月标记第一行,因此我需要使用SQLite函数将时间(以毫秒为单位)转换为任何日期格式.我怎么能避免这个?
是的,我已将System.Data.Sqlite.dll添加到我的项目(VS2012).是的,我添加了一个参考.是的,我已经双重检查,已经创建了引用(参考属性>路径是正确的).是的,我使用过Google,Bing,
不,我不知道为什么我的代码不会编译.
在我的Android应用程序中,有一个预定义的数据库,位于assets文件夹中.我创建了一个表android_metadata,其中包含一个名为locale的列,并且有一个记录en_US.在我的应用程序中,用户应输入他/她的详细信息并单击保存按钮.单击保存按钮时出现以下错误.
10-21 09:37:06.010: E/SQLiteLog(6278): (11) database corruption at line 50741 of [00bb9c9ce4]
10-21 09:37:06.010: E/SQLiteLog(6278): (11) database corruption at line 50780 of [00bb9c9ce4]
10-21 09:37:06.010: E/SQLiteLog(6278): (11) statement aborts at 16: [SELECT locale FROM android_metadata UNION SELECT NULL ORDER BY locale DESC LIMIT 1]
10-21 09:37:06.160: E/SQLiteDatabase(6278): Failed to open database '/data/data/my.easymedi.controller/databases/EasyMediInfo.db'.
10-21 09:37:06.160: E/SQLiteDatabase(6278): android.database.sqlite.SQLiteException: Failed to change locale for db '/data/data/my.easymedi.controller/databases /EasyMediInfo.db' to 'en_US'.
10-21 09:37:06.160: E/SQLiteDatabase(6278): at android.database.sqlite.SQLiteConnection.setLocaleFromConfiguration(SQLiteConnection.java:386)
10-21 09:37:06.160: E/SQLiteDatabase(6278): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:218)
10-21 09:37:06.160: E/SQLiteDatabase(6278): at …Run Code Online (Sandbox Code Playgroud)