在android中选择查询
我试图从表product中选择数据,但在编译时发现“android.database.sqlite.SQLiteException:没有这样的表:product(代码1):”错误在android中选择查询,android,sql,sqlite,Android,Sql,Sqlite,我试图从表product中选择数据,但在编译时发现“android.database.sqlite.SQLiteException:没有这样的表:product(代码1):”错误 public Cursor getproduct(){ SQLiteDatabase db=this.getReadableDatabase(); Cursor cur = null; try{ cur=db.rawQuery("SELECT * FROM product",n
public Cursor getproduct(){
SQLiteDatabase db=this.getReadableDatabase();
Cursor cur = null;
try{
cur=db.rawQuery("SELECT * FROM product",new String [] {});
cur.moveToFirst();
}
catch (Exception e) {
e.printStackTrace();
}
return cur;
}
我建议使用静态引用,例如表名,以免在这方面出错。您的表的onCreate()方法(关于其他人也在引用的内容)应该是这样的:
public class DbHelper extends SQLiteOpenHelper
{
private static String DATABASE_NAME = "FodoSubstitutes.db";
private static String FOOD_TABLE = "Food";
//Creates the database with db name and calls onCreate().
public DbHelper(Context context)
{
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db)
{
//System.out.println("in onCreate");
//assocID food healthierFood category description count submittedBy
String sql = "CREATE TABLE IF NOT EXISTS " + FOOD_TABLE +
"(Food_ID integer primary key, " +
"Food_Name string not null, " +
"Food_Aliases string, " +
"Hints string, " +
"Category string, " +
"Subcategory string, " +
"Description string, " +
"Submission_ID int, " +
"Comment_ID int, " +
"Count int); ";
db.execSQL(sql);
}
}
问题是getReadableDatabase()方法没有返回所需的数据库。您需要在应用程序内部构建它,或者将其与应用程序捆绑(使用资产)。以下是执行这两项操作的代码段: 建立数据库
static final string DATABASE_CREATE = "create table if not exists " + "product " + "(" +
TABLE_COLUMN_1 + " " + COLUMN_1_TYPE + ", " +
TABLE_COLUMN_2 + " " + COLUMN_2_TYPE + ", "; //and so on
public class MySQLHelper extends SQLiteOpenHelper {
MySQLHelper(Context context) {
super(context, YOUR_DATABASE_NAME, null, YOUR_DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(DATABASE_CREATE);
}
catch(SQLException e) {
e.printStackTrace();
}
}
使用资产复制预建数据库
static final string DATABASE_CREATE = "create table if not exists " + "product " + "(" +
TABLE_COLUMN_1 + " " + COLUMN_1_TYPE + ", " +
TABLE_COLUMN_2 + " " + COLUMN_2_TYPE + ", "; //and so on
public class MySQLHelper extends SQLiteOpenHelper {
MySQLHelper(Context context) {
super(context, YOUR_DATABASE_NAME, null, YOUR_DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(DATABASE_CREATE);
}
catch(SQLException e) {
e.printStackTrace();
}
}
首先,您需要将SQL文件放在应用程序的资产文件夹中。然后将此代码放入主活动的onCreate()方法中:
File f = new File(this.getDatabasePath().getAbsolutePath() + "/mydatabase.db";
if(!f.exists()) {
if(f.getParentFile().exists() || f.getParentFile().mkdirs()) {
f.createNewFile();
InputStream inputStream = getBaseContext().getAssets().open("mydatabase.db");
OutputStream outputStream = new FileOutputStream(f);
byte[] buffer = new byte[1024];
int length;
while((length = dbStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.flush();
outputStraem.close();
}
else
throw new IOException("Database input error");
}
}
这是使用一种假设,即预构建的数据库被放置在“mydatabase.db”文件中。您在查询中写入的表名可能有错误,表名可能拼写错误。您需要显示数据库类的
onCreate()
代码只需编写类似cur=db.rawQuery的简单rawQuery即可(“SELECT*FROM product”);
因为您没有指定任何特定的列。您在productDB类中有onCreate?我建议使用它来创建和访问数据库。它可以明显加快开发速度,并防止简单查询中的键入错误。我使用sqlite studio创建数据库,在此应用程序中只使用db