我有一个名为Awal.java的活动和代码:
public class Awal extends Activity implements OnItemClickListener {
private Cursor kategori;
private MyDatabase db;
private List<String> ktg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new MyDatabase(this, null);
ktg = db.getKategori();
String namaKtg[] = ktg.toArray(new String[ktg.size()]);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(new ArrayAdapter<String>(this, R.layout.list, R.id.label, namaKtg));
listView.setOnItemClickListener(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
kategori.close();
db.close();
}
public void onItemClick(AdapterView<?> adapter, View v, int pos, long l) {
Intent intent = new Intent(Awal.this,Detail.class);
intent.putExtra("namaKategori", adapter.getItemAtPosition(pos).toString());
startActivity(intent);
}
}
以及名为MyDatabase.java的databaseHelper,这里是代码:
package com.mroring.belajarperancis;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDatabase extends SQLiteOpenHelper {
// Variable declaration
private static String DB_PATH = "/data/data/com.mroring.belajarperancis/databases/";
public static String DB_NAME = "MY_DATABASE";
private final Context myContext;
private String strMypath;
public MyDatabase(Context context, String DB_NAME) {
super(context, DB_NAME, null, 1);
this.myContext = context;
try {
MyDatabase.DB_NAME = DB_NAME;
} catch (Exception e) {
e.printStackTrace();
}
}
public void createDatabase() throws IOException {
try {
// check if the database exists
boolean dbExist = checkDatabase();
if (!dbExist) {
// database is not present copy databse
this.getReadableDatabase();
try {
copyDatabse(DB_NAME);
} catch (IOException e) {
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 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 doesn't
*/
private boolean checkDatabase() {
try {
File dbFile = new File(DB_PATH + DB_NAME);
return dbFile.exists();
} catch (Exception e) {
e.printStackTrace();
}
return 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.
* */
public void copyDatabse(String DB_NAME) throws IOException {
try {
// Open your local db as the input stream
Input`enter code here`Stream 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 * 2];
int length;
while ((length = myInput.read(buffer)) > 0) {
try {
myOutput.write(buffer, 0, length);
} catch (Exception e) {
}
}
if (myOutput != null) {
myOutput.flush();
myOutput.close();
}
if (myInput != null)
myInput.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* This function is used to open the database
*
* @throws SQLException
*/
public void openDatabase() throws SQLException {
SQLiteDatabase checkDB = null;
// Open the database
try {
strMypath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(strMypath, null,
SQLiteDatabase.OPEN_READWRITE);
} catch (Exception e) {
e.printStackTrace();
}
if (checkDB != null) {
checkDB.close();
}
}
public List<String> getKategori() {
String query = "SELECT _id FROM kategori";
List<String> kategori = new ArrayList<String>();
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery(query, null);
//looping through
if (cursor.moveToFirst()) {
do {
kategori.add(cursor.getString(0)); // add id & nama
}
while (cursor.moveToNext());
}
cursor.close();
db.close();
return kategori;
}
public List<List<String>> getDetail(String namaKategori) {
String query = "select pra, ina, baca from kata,kategori where kata.id_kategori=kategori._id and kategori.nama = '"+namaKategori+"'";
List<List<String>> detail = new ArrayList<List<String>>();
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery(query, null);
//looping
if (cursor.moveToFirst()) {
do {
List<String> item = new ArrayList<String>();
item.add(cursor.getString(0)); //add french word
item.add(cursor.getString(1)); //add indonesian word
item.add(cursor.getString(2)); //add baca
detail.add(item);
}
while (cursor.moveToNext());
}
cursor.close();
db.close();
return detail;
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
try {
copyDatabse(DB_NAME);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
这个组合总会出错,这就是错误:
06-18 01:23:01.694: E/AndroidRuntime(17219): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mroring.belajarperancis/com.mroring.belajarperancis.Awal}: android.database.sqlite.SQLiteException: no such table: kategori: , while compiling: SELECT _id FROM kategori
06-18 01:23:01.694: E/AndroidRuntime(17219): Caused by: android.database.sqlite.SQLiteException: no such table: kategori: , while compiling: SELECT _id FROM kategori
我有一个名为&#39; MY_DATABASE&#39;在... / BelajarPerancis / assets / databases / MY_DATABASE以及... / BelajarPerancis / assets / MY_DATABASE(我试图将数据库放在两个地方)。
我试图从DDMS中提取数据库,它返回相同的数据库,内容/表格相同,并且有表格&#39; Kategori&#39;。
我的&#39; Kategori&#39;表格包含&#39; _id&#39;这是证据3 Rows returned from: select nama from kategori; (took 5ms)
。
答案 0 :(得分:0)
您确定要创建数据库吗?
我之所以这么说是因为您使用绝对路径来获取数据库文件,在这种情况下最好的办法是创建一个数据库作为Google的标准。
这是一个很好的答案,如何做到:
How do I create a database in android?
您也可以检查表格的名称,如果您使用大写的第一个字母,则必须始终以这种方式使用。
ATT。
答案 1 :(得分:0)
db = new MyDatabase(this, null);
String query = "SELECT _id FROM kategori";
哪些不存在.. 将db = new MyDatabase(this, null);
更改为
db = new MyDatabase(this, "SOME_DATABASE_NAME");
然后编写代码以在数据库中创建表。
活动启动无效,因为从db.getKategori();
方法调用了onCreate()
并且正在抛出异常。
请浏览http://www.vogella.com/tutorials/AndroidSQLite/article.html
希望这可以帮助你!!