我正在使用内容提供程序,需要查询一个表,其中row = thing1的一列和同一行的另一列= thing2.我知道你可以查询一个表只有一个条件,如:
Cursor c = contentResolver.query(content_uri, projection, variable + "='" + thing1 + "'", null, null);
Run Code Online (Sandbox Code Playgroud)
现在我如何查询两个条件?任何帮助,将不胜感激.
这是架构:

SQL查询是:SELECT*FROM unjdat其中COL_1 = " myWord ";
即,我想显示col_1为myWord的行的所有列.
int i;
String temp;
words = new ArrayList<String>();
Cursor wordsCursor = database.rawQuery("select * from unjdat where col_1 = \"apple\" ", null); //myWord is "apple" here
if (wordsCursor != null)
wordsCursor.moveToFirst();
if (!wordsCursor.isAfterLast()) {
do {
for (i = 0; i < 11; i++) {
temp = wordsCursor.getString(i);
words.add(temp);
}
} while (wordsCursor.moveToNext());
}
words.close();
Run Code Online (Sandbox Code Playgroud)
我认为问题在于循环.如果我删除for循环并执行wordsCursor.getString(0)它的工作.如何循环获取所有列?
注意:
我目前有一个Rails项目,我将其部署到使用postgres数据库的生产服务器上。我在Windows中开发我的rails项目,这意味着,如果要在本地进行测试,则必须将database.yml文件中的所有数据库从postgres更改为sqlite3(因为将Windows设置为运行postgres服务器似乎是痛苦)。
我想做的是格式化我的database.yml像这样:
development:
adapter: postgresql
encoding: utf8
database: <%= begin IO.read("/home/www-data/.db/.dev_name") rescue "" end %>
pool: 5
username: <%= begin IO.read("/home/www-data/.db/.user") rescue "" end %>
password: <%= begin IO.read("/home/www-data/.db/.pass") rescue "" end %>
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: postgresql
encoding: …Run Code Online (Sandbox Code Playgroud) database sqlite ruby-on-rails ruby-on-rails-3 rails-postgresql
我将实现该方法来检索字符串的记录,但是当涉及到执行时,它会像Log cat所说的那样变成运行时错误.您能否告诉我们如何正确初始化光标?
logcat的
07-08 10:10:34.620: D/result(8036): true
07-08 10:10:34.660: D/memalloc(8036): ion: Unmapping buffer base:0x57c06000 size:466944
07-08 10:10:34.660: D/memalloc(8036): ion: Unmapping buffer base:0x57d6c000 size:466944
07-08 10:10:34.660: D/memalloc(8036): ion: Unmapping buffer base:0x57cbb000 size:466944
07-08 10:10:34.710: D/debug(8036): Order 87318702
07-08 10:10:34.710: D/pcDbHelper(8036): pcDbHelper com.example.recordandmovie.PhotoDbAdapter@412cad30
07-08 10:10:34.841: W/CursorWindow(8036): Window is full: requested allocation 1249663 bytes, free space 398939 bytes, window size 2097152 bytes
07-08 10:10:34.931: W/CursorWindow(8036): Window is full: requested allocation 1249663 bytes, free space 398939 bytes, window size 2097152 bytes
07-08 10:10:34.941: D/debug …Run Code Online (Sandbox Code Playgroud) 这是我正在使用的代码(在许多答案中找到):
InputStream myInput;
try {
myInput = iNezamApplication.getAppContext().getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
} catch (IOException e1) {
e1.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
但是,在到达OutpoutStream行后,我总是遇到异常:
java.io.FileNotFoundException: /data/data/package_name/databases/databasename.db: open failed: ENOENT (No such file or directory)
Run Code Online (Sandbox Code Playgroud) 假设我有一个表中STUDENT的列,包括ID,name,email,和advisorID.我有第二个表格ADVISOR,其中包括ID和name.STUDENT.AdvisorID是一把外键.插入学生时,必须有一个指向一个学生的键ADVISOR.
但是,从应用程序插入学生时,您只有一个ADVISOR名称.换句话说,您的字符串包含特定学生的姓名和电子邮件,以及顾问的姓名,但不包括顾问ID.
不知何故,我无法理解如何做到这一点.我知道这是一种常见的情况,我甚至在堆栈溢出时发现了一两个类似的问题.我知道我们正在谈论INSERT INTO ... SELECT ... FROM .. WHERE.我假设WHERE子句是advisor.name ='anAdvisorName',但我正在努力解决这样一个事实:插入的一些信息来自一个表,而另一些则不是.(关于这种陈述的讨论似乎集中在所有插入的数据来自第二个表的情况.这不是我所说的.)
ps为简单起见,请不要担心重复的条目,多个具有相同名称的顾问等.
这是我的示例数据(没有索引,我不想创建任何索引):
CREATE TABLE tblTest ( a INT , b INT );
INSERT INTO tblTest ( a, b ) VALUES
( 1 , 2 ),
( 5 , 1 ),
( 1 , 4 ),
( 3 , 2 )
Run Code Online (Sandbox Code Playgroud)
我希望列a和列b的最小值都大于给定值.例如,如果给定的值是3,那么我想要返回4.
这是我目前的解决方案:
SELECT MIN (subMin) FROM
(
SELECT MIN (a) as subMin FROM tblTest
WHERE a > 3 -- Returns 5
UNION
SELECT MIN (b) as subMin FROM tblTest
WHERE b > 3 -- Returns 4
)
Run Code Online (Sandbox Code Playgroud)
这会搜索表两次 - 一次得到min(a) …
我有一个现有的sqlite数据库.我正在开发一个Android应用程序,我想将它与现有的sqlite DataBase连接.
问题1: 根据我的讲师的建议,我已经通过"DDMS推送数据库功能"在我的项目中包含了sqlite数据库.现在我想从数据库中获取数据,我是否需要使用SQLiteOpenHelper.如果是,如何使用它以及将在onCreate(SQLiteDatabase db)函数和onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)函数中编码的函数,因为我们已经拥有了数据库,我们并不需要创建它.
问题2: 如何从现有数据库中获取所需数据应该怎么做.
作为一个新手,我很困惑,有人可以解释这些概念,并指导我克服这个问题.任何帮助,将不胜感激.
我已经看到了@TronicZomB提出的教程,但是根据本教程(http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/) ,我必须将所有带有主键字段的表作为_id.
我有7个表,即目的地,事件,旅游,tour_cat,tour_dest,android_metadata和sqlite_sequence.总而言之,只有tour_dest不满足具有名为_id的主键的条件.怎么弄清楚这一个?
以下是表的屏幕截图,该表缺少绑定数据库表的id字段所需的主键字段.

我没有在http://www.sqlite.org/download.html上找到任何针对Android或iOS的SQLite下载,但我确实找到了Windows Phone 8的二进制下载.
只是出于好奇,看起来像一个distinct领域必须放在任何其他领域之前,我错了吗?
在SQLite中查看此示例,
sqlite> select ip, distinct code from parser; # syntax error?
Error: near "distinct": syntax error
sqlite> select distinct code, ip from parser; # works
Run Code Online (Sandbox Code Playgroud)
这是为什么?我真的有语法错误吗?