Java 我的程序在Android上的sqlite数据库中找不到表
我有SQLite数据库文件(我没有在这个程序中创建它,它有它的表和数据),我在我的android程序中打开它,但是当我编写SELECT语句时,程序找不到表,我得到错误: 错误:没有这样的表:Person 这是代码:Java 我的程序在Android上的sqlite数据库中找不到表,java,android,sqlite,select,Java,Android,Sqlite,Select,我有SQLite数据库文件(我没有在这个程序中创建它,它有它的表和数据),我在我的android程序中打开它,但是当我编写SELECT语句时,程序找不到表,我得到错误: 错误:没有这样的表:Person 这是代码: 公共类SQLiteAdapter{ 私有数据库助手数据库助手; 私有静态字符串dbfile=“/data/data/com.example.searchpersons/databases/”; 私有静态字符串DB_NAME=“Person.DB”; 静态字符串myPath=dbfil
公共类SQLiteAdapter{
私有数据库助手数据库助手;
私有静态字符串dbfile=“/data/data/com.example.searchpersons/databases/”;
私有静态字符串DB_NAME=“Person.DB”;
静态字符串myPath=dbfile+DB\u NAME;
私有静态数据库;
私有静态最终int数据库_VERSION=3;
私有静态字符串table=“Person”;
私有静态上下文myContext;
公共SQLiteAdapter(上下文ctx){
SQLiteAdapter.myContext=ctx;
databaseHelper=新的DbDatabaseHelper(SQLiteAdapter.myContext);
}
公共静态类DbDatabaseHelper扩展了SQLiteOpenHelper{
公共DbDatabaseHelper(上下文){
super(上下文、数据库名称、null、数据库版本);
dbfile=“/data/data/”+context.getPackageName()+”/databases/”;
myPath=dbfile+DB\u名称;
//this.myContext=上下文;
}
@凌驾
public void onCreate(SQLiteDatabase db){
}
@凌驾
public void onUpgrade(SQLiteDatabase db,int-oldVersion,int-newVersion){
}
}
公共SQLiteDatabase open(){
试一试{
database=SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN\u READONLY);
Log.v(“数据库日志”,“数据库存在打开”);
}catch(sqlitee异常){
Log.v(“数据库日志”,“数据库不存在”);
}
if(database!=null&&database.isOpen()
返回数据库;
否则{
database=databaseHelper.getReadableDatabase();
Log.v(“数据库日志”、“数据库存在助手”);
}
返回数据库;
}
选择上的公共光标(字符串名、字符串名){
Log.v(“数据库日志”、“数据库存在选择”);
游标c=database.rawQuery(“从“+table+”中选择*,其中Firstname=”“+Firstname+”,Lastname=”“+Lastname+””,null);
c、 moveToFirst();
返回c;
}
公众假期结束(){
if(database!=null&&database.isOpen(){
close()数据库;
}
}
}
这是按钮点击功能:
@覆盖
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
最终视图根视图=充气机。充气(R.layout.fragment_main,容器,假);
按钮btn1=(按钮)rootView.findViewById(R.id.button1);
btn1.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
EditText t=(EditText)rootView.findviewbyd(R.id.editText1);
字符串名称=t.getText().toString();
EditText tt=(EditText)rootView.findViewById(R.id.editText2);
字符串lastname=tt.getText().toString();
if(name.length()==0 | | lastname.length()==0){
Toast.makeText(rootView.getContext(),“请填写两个框”,Toast.LENGTH\u LONG.show();
}否则{
GridView GridView=(GridView)rootView.findviewbyd(R.id.gridView1);
Listli=newarraylist();
ArrayAdapteradapter=new ArrayAdapter(rootView.getContext(),android.R.layout.simple_gallery_项目,li);
试一试{
SQLiteAdapter s=新的SQLiteAdapter(rootView.getContext());
s、 open();
光标c=s.onSelect(name,lastname);
如果(c!=null){
if(c.moveToFirst()){
做{
String id=c.getString(c.getColumnIndex(“id”);
字符串名称1=c.getString(c.getColumnIndex(“Firstname”);
字符串lastname1=c.getString(c.getColumnIndex(“Lastname”);
String personal=c.getString(c.getColumnIndex(“PersonalID”);
li.添加(id);
添加(名称1);
加上(姓氏1);
加上(个人);
setAdapter(适配器);
}而(c.moveToNext());
}
}否则{
Toast.makeText(rootView.getContext(),“没有数据”,Toast.LENGTH_LONG.show();
}
c、 close();
s、 close();
}捕获(例外e){
Toast.makeText(rootView.getContext(),“Error:+e.getMessage(),Toast.LENGTH_LONG).show();
}
}
}
});
返回rootView;
}
我在SQLite数据库浏览器中检查数据库,一切正常(有表和数据),但程序仍然找不到它们
我将SqlItemManager添加到eclipse中,它也无法查看表:
只有一个表,没有我的表
有人能帮我吗?你把路弄乱了
In oncreate you have to create your db. I am sending you my db class.
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DbAdapter extends SQLiteOpenHelper {
private static DbAdapter mDbHelper;
public static final String DATABASE_NAME = "demoDb";
public static final String TABLE_Coin= "coin_table";
public static final String TABLE_Inbox= "inbox";
public static final String TABLE_Feature= "feature";
public static final String TABLE_Time= "time";
public static final String TABLE_Deduct_money= "deduct_time";
public static final String TABLE_Unread_message= "unread_message";
public static final String COLUMN_Email= "email";
public static final String COLUMN_Appearence= "appearence";
public static final String COLUMN_Drivability= "drivability";
public static final String COLUMN_Fuel= "fuel";
public static final String COLUMN_Insurance= "insurance";
public static final String COLUMN_Wow= "wow";
public static final String COLUMN_CurrentValue= "current_value";
public static final String COLUMN_coin = "name";
public static final String COLUMN_seenTime = "seen";
public static final String COLUMN_number_of_times = "number_of_times";
public static final String COLUMN_name = "name";
public static final String COLUMN_type = "type";
public static final String COLUMN_text = "text";
public static final String COLUMN_image = "image";
public static final String COLUMN_created_time = "created_time";
public static final String COLUMN_unread = "unread";
// ****************************************
private static final int DATABASE_VERSION = 1;
private final String DATABASE_CREATE_BOOKMARK = "CREATE TABLE "
+ TABLE_Coin + "(" + COLUMN_coin
+ " Varchar,"+COLUMN_Email +" Varchar, UNIQUE("
+ COLUMN_Email + ") ON CONFLICT REPLACE)";
private final String DATABASE_CREATE_BOOKMARK1 = "CREATE TABLE "
+ TABLE_Feature + "(" + COLUMN_Appearence
+ " Integer,"+COLUMN_Email +" Varchar ,"+COLUMN_name +" Varchar ,"+COLUMN_Drivability +" Integer ,"+COLUMN_Wow +" Integer,"+COLUMN_CurrentValue +" Integer,"+COLUMN_Fuel +" Integer,"+COLUMN_Insurance +" Integer, UNIQUE("
+ COLUMN_Email + ") ON CONFLICT REPLACE)";
private final String DATABASE_CREATE_BOOKMARK2 = "CREATE TABLE "
+ TABLE_Time + "(" + COLUMN_Email +" Varchar ,"+COLUMN_seenTime +" Integer,"+COLUMN_number_of_times +" Integer, UNIQUE("
+ COLUMN_Email + ") ON CONFLICT REPLACE)";
private final String DATABASE_CREATE_BOOKMARK3 = "CREATE TABLE "
+ TABLE_Deduct_money + "(" + COLUMN_seenTime
+ " Varchar,"+ COLUMN_number_of_times
+ " Integer,"+COLUMN_Email +" Varchar, UNIQUE("
+ COLUMN_Email + ") ON CONFLICT REPLACE)";
private final String DATABASE_CREATE_BOOKMARK4 = "CREATE TABLE "
+ TABLE_Inbox + "(" + COLUMN_created_time
+ " DATETIME,"+ COLUMN_image
+ " Varchar,"
+ COLUMN_type
+ " Varchar,"+ COLUMN_name
+ " Varchar,"+ COLUMN_text
+ " Varchar,"+COLUMN_Email +" Varchar)";
private final String DATABASE_CREATE_BOOKMARK5 = "CREATE TABLE "
+ TABLE_Unread_message + "(" + COLUMN_unread
+ " Varchar,"+COLUMN_Email +" Varchar, UNIQUE("
+ COLUMN_Email + ") ON CONFLICT REPLACE)";
private DbAdapter(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public static synchronized DbAdapter getInstance(Context context) {
if (mDbHelper == null) {
mDbHelper = new DbAdapter(context);
}
return mDbHelper;
}
/**
* Tries to insert data into table
*
* @param contentValues
* @param tablename
* @throws SQLException
* on insert error
*/
public void insertQuery(ContentValues contentValues, String tablename)
throws SQLException {
try {
final SQLiteDatabase writableDatabase = getWritableDatabase();
writableDatabase.insert(tablename, null, contentValues);
// writableDatabase.insertWithOnConflict(tablename, null,
// contentValues,SQLiteDatabase.CONFLICT_REPLACE);
} catch (Exception e) {
e.printStackTrace();
}
}
// public void insertReplaceQuery(ContentValues contentValues, String tablename)
// throws SQLException {
//
// try {
// final SQLiteDatabase writableDatabase = getWritableDatabase();
// writableDatabase.insertOrThrow(tablename, null, contentValues);
//
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
//
// /**
// * Update record by ID with contentValues
// *
// * @param id
// * @param contentValues
// * @param tableName
// * @param whereclause
// * @param whereargs
// */
public void updateQuery(ContentValues contentValues, String tableName,
String whereclause, String[] whereargs) {
try {
final SQLiteDatabase writableDatabase = getWritableDatabase();
writableDatabase.update(tableName, contentValues, whereclause,
whereargs);
} catch (Exception e) {
e.printStackTrace();
}
}
public Cursor fetchQuery(String query) {
final SQLiteDatabase readableDatabase = getReadableDatabase();
final Cursor cursor = readableDatabase.rawQuery(query, null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
public Cursor fetchQuery(String query, String[] selectionArgs) {
final SQLiteDatabase readableDatabase = getReadableDatabase();
final Cursor cursor = readableDatabase.rawQuery(query, selectionArgs);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
public void delete(String table) {
final SQLiteDatabase writableDatabase = getWritableDatabase();
writableDatabase.delete(table, null, null);
}
public void delete(String table, String whereClause, String[] selectionArgs) {
final SQLiteDatabase writableDatabase = getWritableDatabase();
writableDatabase.delete(table, whereClause, selectionArgs);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE_BOOKMARK);
db.execSQL(DATABASE_CREATE_BOOKMARK1);
db.execSQL(DATABASE_CREATE_BOOKMARK2);
db.execSQL(DATABASE_CREATE_BOOKMARK3);
db.execSQL(DATABASE_CREATE_BOOKMARK4);
db.execSQL(DATABASE_CREATE_BOOKMARK5);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_Coin);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_Feature);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_Time);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_Deduct_money);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_Inbox);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_Unread_message);
onCreate(db);
}
}
请清理对dbfile
和myPath
的每个定义或引用。您正在使用一些值在定义中初始化它们(可能是复制粘贴的),然后在DbDatabaseHelper
构造函数中为它们提供新的不同值。而且助手不会使用这些路径,它只会在默认目录中创建数据库
然后,在open
方法中调用SQLiteDatabase.openDatabase
wit