gpt4 book ai didi

java - 无法从 SQLite 检索数据

转载 作者:行者123 更新时间:2023-12-01 17:26:07 24 4
gpt4 key购买 nike

请看下面的代码

DatabaseHandler.java

用于检索数据的代码

public List<String> getAllBranches() {
// TODO Auto-generated method stub

String selectQuery = "select city from BranchNetwork";

Cursor cursor = database.rawQuery(selectQuery, null);
List <String>branches = new ArrayList<String>();

if(cursor.isFirst())
{
do
{

}
while(cursor.moveToNext());

Toast.makeText(context, "Data Retrieved: "+branches.get(1), Toast.LENGTH_LONG).show();
}

return branches;
}

用于插入数据的代码

public String insertData(String city, String streetAddress,String phoneNumber1, String phoneNumber2, String email) 
{
String insertQuery = "insert into BranchNetwork ('city','streetAddress','phoneNumber1','phoneNumber2','email') values('"+city+"','"+streetAddress+"','"+phoneNumber1+"','"+phoneNumber2+"','"+email+"');";




try
{
database.execSQL(insertQuery);


return "Data Successfully Inserted";

}
catch(Exception e)
{
Toast.makeText(context, "Exception: "+e.getMessage(), Toast.LENGTH_LONG).show();
return "Data Insertion Failed";
}

}

Form.java

第一个 Activity 。这里我检索数据并插入数据。以下是我检索数据的方法

public class Form extends Activity {
private List<String>branches = new ArrayList<String>();
private DatabaseConnector databaseConnector;
String[]arr;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form);

databaseConnector = DatabaseHandler.getInstance(this);
databaseConnector.createConnection();

branches = databaseConnector.getAllBranches();

arr = branches.toArray(new String[branches.size()]);
}
}

但是,我什么也没得到!在 DatabaseHandler.java 中,我创建了一个 Toast,在检索代码时它应该生成一条消息。但它没有产生任何东西!我正在将数据加载到 ListView 中,但它也是空的!为什么是这样?请帮忙!

PS:以下是DatabaseConnector.java,DatabaseHandler的插入面

package com.example.esoftcallmanager;

import java.util.List;

import android.content.Context;

public interface DatabaseConnector
{
public void createConnection();
public void closeConnection();
public String getPhoneNumber();
public String insertData(String city, String streetAddress, String phoneNumber1, String phoneNumber2, String email);
public List<String>getAllBranches();

}

最佳答案

如果您确定数据库已有数据,请尝试将 if(cursor.isFirst()) 更改为 if(cursor.moveToFirst())

关于java - 无法从 SQLite 检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14870144/

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