大家好,我一直在尝试使用数据库并将其信息显示在listView中,但我无法使其正常工作。 这是我的DatabaseHelper类(它从SQLiteOpenHelper扩展):
FBSDKCoreKit.h
FBSDK_EXTERN NSString* const EMAIL;
这是我的ActivityMain:`
public static String DB_PATH = "/data/data/ice.tea09.sqlitedemo/databases/";
public static String DB_NAME = "Test.sqlite";
public static final int DB_VERSION = 1;
public static final String TB_USER = "Users";
private SQLiteDatabase myDB;
private Context context;
//private String DB_PATH =context.getFilesDir().getPath();
public DatabaseHelper(Context context)
{
super(context, DB_NAME, null, DB_VERSION);
this.context = context;
}
@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
}
@Override
public synchronized void close()
{
if(myDB!=null)
{
myDB.close();
}
super.close();
}
public List<String> getAllUsers()
{
List<String> listUsers = new ArrayList<String>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor c;
try
{
c = db.rawQuery("SELECT * FROM " + TB_USER , null);
if(c == null) return null;
String name;
c.moveToFirst();
do
{
name = c.getString(1);
listUsers.add(name);
} while (c.moveToNext());
c.close();
}
catch (Exception e)
{
Log.e("tle99", e.getMessage());
}
db.close();
return listUsers;
}
/*
* Copy database from source code assets to device
* @throws IOException
*/
public void copyDataBase() throws IOException
{
try
{
InputStream myInput = context.getAssets().open(DB_NAME);
String outputFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outputFileName);
byte[] buffer = new byte[1024];
int length;
while((length = myInput.read(buffer))>0)
{
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
catch (Exception e)
{
Log.e("tle99 - copyDatabase", e.getMessage());
}
}
/*
* Check if the database doesn't exist on device, create new one
* @throws IOException
*/
public void createDataBase() throws IOException
{
boolean dbExist = checkDataBase();
if (dbExist)
{
}
else
{
this.getReadableDatabase();
try
{
copyDataBase();
}
catch (IOException e)
{
Log.e("tle99 - create", e.getMessage());
}
}
}
// ---------------------------------------------
// PRIVATE METHODS
// ---------------------------------------------
/*
* Check if the database is exist on device or not
* @return
*/
private boolean checkDataBase()
{
SQLiteDatabase tempDB = null;
try
{
String myPath = DB_PATH + DB_NAME;
tempDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
catch (SQLiteException e)
{
Log.e("tle99 - check", e.getMessage());
}
if (tempDB != null)
{
tempDB.close();
}
return tempDB != null ? true : false;
}
我的资产文件夹中有一个数据库 该应用程序在启动和运行时什么也没有显示 谁能帮我这个忙,请问我真的不知道。
答案 0 :(得分:0)
首先最好使用
获取数据库地址。@SuppressLint("SdCardPath")
private void checkDBAddress(Context context) {
if (android.os.Build.VERSION.SDK_INT >= 17) {
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
} else {
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
}
}
在该行中,您应该从零开始,所以如果您只有一个字段,请更改它
name = c.getString(1);
到
name = c.getString(0);
最后,如果您遇到相同的问题,将适配器改为
,则我在ArrayAdapter上遇到了异常ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);