- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我知道有数十亿个关于 SQL 数据库的线程,但我没有找到任何解决我的问题的线程。我制作了一个名为 Beer Or No Beer 的应用程序这需要一个包含啤酒品牌及其国家/地区的 Arraylist。现在我想用 SQLite 管理这个列表。我使用 SQLite 数据库浏览器创建了一个数据库,并将该数据库复制到 Assets 文件夹(名称:sqbeerlist)中。现在我已经创建了一个 DatabaseHelper 类来打开文件并将其写入数组列表。但是当我想访问行时
Cursor c = myDataBase.rawQuery("SELECT _id, brand FROM " + DATABASE_TABLE, null);
DVM 崩溃并显示未找到源。我做错了什么??
这是整个 HelperClass:
package com.celticwolf.alex;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseHelper extends SQLiteOpenHelper {
// The Androids default system path of your application database.
private static String DB_PATH = "/data/data/com.celticwolf.alex/databases/";
private static String DB_NAME = "sqbeerlist";
private static final String DATABASE_TABLE = "beers";
private SQLiteDatabase myDataBase;
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "brand";
public static final String KEY_COUNTRY = "country";
private final Context myContext;
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
/**
*
* Creates a empty database on the system and rewrites it with your own
*
* database.
*
* */
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
// do nothing - database already exist
} else {
// By calling this method and empty database will be created into
// the default system path
// of your application so we are gonna be able to overwrite that
// database with our database.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
/**
*
* Check if the database already exist to avoid re-copying the file each
*
* time you open the application.
*
*
*
* @return true if it exists, false if it doesnt
*/
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
// database doest exist yet.
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
*
* Copies your database from your local assets-folder to the just created
*
* empty database in the system folder, from where it can be accessed and
*
* handled. This is done by transfering bytestream.
*
* */
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();
}
public ArrayList<String> gettheArray() {
ArrayList<String> result = new ArrayList<String>();
try {
openDataBase();
}catch(SQLException sqle){
throw sqle;
}
Cursor c = myDataBase.rawQuery("SELECT _id, brand FROM " + DATABASE_TABLE, null);
c.moveToFirst();
// Check if our result was valid.
if (c != null) {
// Loop through all Results
do {
result.add(c.getString(c.getColumnIndex(KEY_NAME)));
} while (c.moveToNext());
}
close();
return result;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
// Add your public helper methods to access and get content from the
// database.
// You could return cursors by doing "return myDataBase.query(....)" so it'd
// be easy
// to you to create adapters for your views.
}
下面是我访问类的方式:
DataBaseHelper myDbHelper = new DataBaseHelper(null);
myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
ArrayList<String> thelist = myDbHelper.gettheArray();
谢谢,我希望有人能提供帮助(:
问题已解决!这是正确的代码!这个标志标志着变化:c==3
package com.celticwolf.alex;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseHelper extends SQLiteOpenHelper {
// The Android's default system path of your application database.
private static String DB_PATH = "/data/data/com.celticwolf.alex/databases/";
private static String DB_NAME = "sqbeerlist.sqlite";
private static final String DATABASE_TABLE = "beers";
private SQLiteDatabase myDataBase;
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "brand";
public static final String KEY_COUNTRY = "country";
private final Context myContext;
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
/**
*
* Creates a empty database on the system and rewrites it with your own
*
* database.
*
* */
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
SQLiteDatabase db_Read = null; // c==3
if (dbExist) {
// do nothing - database already exist
} else {
// By calling this method and empty database will be created into
// the default system path
// of your application so we are gonna be able to overwrite that
// database with our database.
db_Read = this.getReadableDatabase(); // c==3
db_Read.close(); // c==3
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
/**
*
* Check if the database already exist to avoid re-copying the file each
*
* time you open the application.
*
*
*
* @return true if it exists, false if it doesnt
*/
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
(SQLiteDatabase.OPEN_READONLY | SQLiteDatabase.NO_LOCALIZED_COLLATORS)); // c==3
} catch (SQLiteException e) {
// database doesnt exist yet.
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
*
* Copies your database from your local assets-folder to the just created
*
* empty database in the system folder, from where it can be accessed and
*
* handled. This is done by transfering bytestream.
*
* */
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 | SQLiteDatabase.NO_LOCALIZED_COLLATORS)); // c==3
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
public ArrayList<String> gettheArray() {
ArrayList<String> result = new ArrayList<String>();
try {
openDataBase();
}catch(SQLException sqle){
throw sqle;
}
Cursor c = myDataBase.rawQuery("SELECT _id, brand FROM " + DATABASE_TABLE, null);
c.moveToFirst();
// Check if our result was valid.
if (c != null) {
// Loop through all Results
do {
result.add(c.getString(c.getColumnIndex(KEY_NAME)));
} while (c.moveToNext());
}
close();
return result;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
// Add your public helper methods to access and get content from the
// database.
// You could return cursors by doing "return myDataBase.query(....)" so itd
// be easy
// to you to create adapters for your views.
}
最佳答案
前段时间我在最初复制数据库时遇到了这个问题。
我最终在第一次运行应用程序时使用 csv 格式的资源( Assets 或原始资源)从头开始创建数据库(显然这可能会占用更多空间)
看看this thread看看是否有帮助。
关于Android SQL 数据库 - 未找到 rawQuery() 源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10106152/
语境 我正在将一个旧的 php 电子商务网站变成一个用 gatsby.js 构建的静态网站。 我将所有产品元数据放入单独的 .json 文件(每个产品一个),并且我能够使用 json 和文件转换器插件
我曾经能够使用三指向上滚动在源/标题之间切换。自从升级到 Lion 后,我只进行常规滚动。有没有办法恢复该功能? Aka,当我像以前那样向上/向下滚动时,它不会跳到对应的位置。 更新 Apple 在
我有一个包含复选框输入的克隆元素。当克隆的元素未被选中时,我需要也取消选中源元素。有没有办法在 jQuery 中做到这一点?或者我是否以错误的方式处理这个问题(即使用clone())?我应该提到我的问
我有一个类,其中有两个 JSpinner 对象,x 和 y。我有一个更改监听器,它已添加到两者中。有人可以告诉我如何实现我的更改监听器,以便监听器可以区分两个对象之间的区别。例如伪代码: if(sou
我正在编写涉及 for 循环的代码,该循环在每个索引处进行计算。 这些计算中的最小值存储在一个变量中,我在程序末尾使用 MPI_Allreduce 来确定所有进程的全局最小值。 但是,我需要一种方法来
我需要在一个 Android 项目中创建一个 map View ,我从服务器获取自定义 map 图 block PNG。有人知道实现此类功能的简单许可 API 吗? 最佳答案 我使用了 OsmDroi
因为我必须创建一个可以更改图像 (src/background-url) 的函数。我想知道如何识别标签以及它是使用 src 还是 url 来访问图像。 让我们说 早些时候我写了一个可以
当我使用源 map 浏览器 https://github.com/danvk/source-map-explorer要检查捆绑包中的内容,我得到以下输出: D:\projects\angular\mT
我正在为客户将 Windev 应用程序移植到 Objective-C。出于显而易见的原因,使用以前的源代码会更简单。 不幸的是,它是加密的,我需要 EDI 才能看到它;完整版的 Windev 太贵了(
我有一个简单的视频播放器,它使用 WPF MediaElement 播放一系列视频。这些视频一起形成一个围绕静止图像移动的连续电影。在每个视频结束时,运动会卡住在当前播放视频的最后一帧。当我按下一个按
我需要更改 openlayer 的图层源(使用 open weather api)。目前我正在使用以下代码但没有成功。 let layer = this.map.getLayers().getArra
我正在尝试在 /dev/random 的机器上运行代码不会很快填满,我正在尝试使用的 Java 程序因缺少随机数而挂起。/dev/urandom产生“不太好”的随机数,但不会阻塞,对于这种情况,我宁愿
我需要 Yocto 项目的源代码包。我已经拥有整个项目的所有资源,但它们还包括开发工具。 我想有一种方法来生成将为目标图像构建的所有包的(修补的)源。因此,例如,如果目标图像包含 busybox,我想
如何对入侵者隐藏 iFrame src 假设我正在流式传输我的网络摄像头或我的电脑屏幕,这是 iframe 代码: 并且我不希望它在大多数浏览器上显示页面源中的流 URL 和检查功能! 这意
是否可以进入 Qt 源,例如qmainwindow.cpp,在 Qt Creator 中?目前我正在看到反汇编,但最好能看到源代码。 最佳答案 当然!但您可能必须首先: 转到 $QT_HOME/qt
我正在尝试创建一个包含很少动漫剧集的简单网站。我有一个关于 javascript 的问题。如何通过单击我的链接之一来更改视频源?我明白,我必须使用事件监听器,只需更改 取决于我点击的链接,但我不太擅长
我有一个带有 BindingSouce 的 DevExpress GridControl。我想清除 BindingSource 并用新数据填充它。我这样做: var list = new List()
当单击提交输入按钮时,我尝试将其他参数(选定复选框的列表)传递到服务器处理的 DataTables 表#my_table: 这可能意味着我必须将 my_table.sAjaxSource 设置为后端脚
(好吧,别对我大喊大叫,这里已经很晚了:)) 我正在研究 delta diff 工具(命令行工具或组件,只要我可以从 Delphi 2010 调用它们就可以了) 我有这个项目,我将文件上传到服务器,我
我需要解析 Yahoo Weather RSS feed 中的某个位置,例如 http://weather.yahooapis.com/forecastrss?w=44418&u=c例如,获取最高、最
我是一名优秀的程序员,十分优秀!