需要帮助从 SQLite 数据库中获取数据
我正在创建一个应用程序,在谷歌地图上显示某些咖啡馆的位置。我希望我的应用程序从数据库中获取咖啡馆的经度和纬度,并使用它们在地图上设置标记。
我已经阅读了很多关于如何使用 SQLite 的教程,但我只能打开我的数据库,但无法检索任何内容。我尝试过使用光标等,但不断出现错误。我相信这是因为缺乏知识。我浏览过这个网站,但找不到任何对我有用的东西。感谢本教程,我走到了这一步:[http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/comment-page-5/#comment-62428][ 1]
这就是我的 dbhelper 的样子:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
public class Databasehelper extends SQLiteOpenHelper{
private static String DB_PATH = "/data/data/com.map.mapguide/databases/";
private static String DB_NAME = "mapDB";
private SQLiteDatabase myDataBase;
private final Context myContext;
public Databasehelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
}else{
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
public boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
在我的主类中扩展了 MapActivity。这是我控制地图上的动作的地方。如何从数据库中检索数据并在主类中设置经度和纬度。
我真的很感谢你的帮助。我已经这样做了大约 2-3 天,但仍然无法弄清楚我做错了什么。 [1]: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/comment-page-5/#comment-62428
非常感谢进步!
巴米
I'm creating an application that shows the location of certain cafes on google maps. I want my app to get the longitude and latitude of the cafes out of a database and use them to set markers on my map.
I've gone through so many tutorials on how to use SQLite and I've only managed to open my database but not to retrieve anything. I've tried using cursor and such but keep getting errors. I believe this is because of the lack of knowledge. I've surfed through this website and couldn't find anything useful for me. I got this far thanks to this tutorial: [http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/comment-page-5/#comment-62428][1]
This how my dbhelper looks like:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
public class Databasehelper extends SQLiteOpenHelper{
private static String DB_PATH = "/data/data/com.map.mapguide/databases/";
private static String DB_NAME = "mapDB";
private SQLiteDatabase myDataBase;
private final Context myContext;
public Databasehelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
}else{
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
public boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
In my main class extends MapActivity. This is were i control the actions on my map. How do i retrieve data from my database and set the longitude and latitude in my main class.
I really appreciate the help. I've been on this for about 2-3 days and still can't figure out what I am doing wrong. [1]: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/comment-page-5/#comment-62428
Thanks a lot in advance!
Bami
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个 devx 教程可能会对您有所帮助。逐步了解如何使用 Log 无限制地工作。 warndesign 和 devx 教程都给了我很多帮助,现在我有一个外部初始化的多表数据库,包装在 ContentProvider 中,它的工作方式就像一个魅力。
首先尝试获取所有数据并将光标与简单的 ListActivity 一起使用。如果这不起作用,并且只要您不真正理解如何操作,那么增加更多的复杂性就没有意义。
我的两分钱。
--- 更新:
改编自我的 ContentProvider 类,
然后尝试在 myDB 上查询
然后您可以控制记录
c.getCount()
的结果,并使用c 测试第一个元素的内容。 moveToFirst()。
This devx tutorial may help you. Proceed step by step to understand how it works using Log without restriction. Both reigndesign and devx tutorials helped me a lot and now I have an externally initialized multi-tables database wrapped in a ContentProvider and it works like a charm.
First try to get all your data and use the cursor with a simple ListActivity. If this does not work and as long as you don't really understand how, it's pointless to add more complexity.
My two cents.
−−− Update:
Adapted from my ContentProvider class, try
then query on myDB
You could then control the result on logging
c.getCount()
and test the content of the first element withc.moveToFirst()
.