gpt4 book ai didi

android - "How to retrieve data from multiple tables using cursors in android studio"

转载 作者:行者123 更新时间:2023-11-30 00:55:46 24 4
gpt4 key购买 nike

我尝试使用游标从两个表中检索数据,但我只从一个表中获取数据。请帮忙。

这是完整的代码。

//创建表

//表T24

db.execSQL("CREATE TABLE IF NOT EXISTS T24 (ID NUMBER PRIMARY KEY,STARTING VARCHAR,DESTINATION VARCHAR" +
",PLACES VARCHAR,PLACESFARE NUMBER(9,3),HOTELS VARCHAR,HOTELSFARE NUMBER(9,3),TRANSPORT VARCHAR," +
"TRANSPORTFARE VARCHAR,DAYS INTEGER,TOTALFARE NUMBER(9,3))");

//表P21

 db.execSQL("CREATE TABLE IF NOT EXISTS P21"+"(NAME VARCHAR PRIMARY KEY,NO NUMBER," +
"ADDRESS VARCHAR,DAYS NUMBER,DATE NUMBER,MONTH NUMBER)");

//从数据库中获取数据。

 db = openOrCreateDatabase("tour", Context.MODE_PRIVATE, null);
String query1 = "select *from T24 where " +
"STARTING='" + s3 + "' and DESTINATION='" + s4 + "'";
Cursor c = db.rawQuery(query1, null);
if (c.moveToFirst() == true) {
t1 = c.getString(3);
t2 = c.getString(4);
t3 = c.getString(5);
t4 = c.getString(6);
t5 = c.getString(7);
t6 = c.getString(8);
t7 = c.getString(9);
t8 = c.getString(10);
}
c.close();
db.close();
ptv.setText(t1);
pf.setText(t2);
hotels.setText(t3);
hf.setText(t4);
td.setText(t5);
tf.setText(t6);
fare.setText(t8);
try {
db2 = openOrCreateDatabase("tour", Context.MODE_PRIVATE, null);
String query2 = "select *from P21 where" +
"NAME='" + e1 + "'";
Cursor c1 = db2.rawQuery(query2, null);
if (c1.moveToFirst() == true) {
t9 = c1.getString(0);
t10 = c1.getString(1);
t11 = Integer.parseInt(c1.getString(3));
t12 = Integer.parseInt(c1.getString(4));
t13 = Integer.parseInt(c1.getString(5));
}
c1.close();
db2.close();
Toast.makeText(getApplicationContext(), "done3", Toast.LENGTH_LONG).show();
name.setText(t9);
no.setText(t10);
sdate1.setText(t12);
sdate2.setText(t13);

我能够从表“T24”中检索日期,但无法从表“P21”中检索数据。

请帮忙。

最佳答案

我想你在P21的sqlite语句中的“where”后面留了空格:

String query2 = "select *from P21 where" +
"NAME='" + e1 + "'";

你需要这样写:

String query2 = "select * from P21 where " +
"NAME='" + e1 + "'";

关于android - "How to retrieve data from multiple tables using cursors in android studio",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40189223/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com