package AHNdroid.PhoneBook;
import android.app.*;
import android.content.*;
import android.database.*;
import android.database.sqlite.*;
import android.os.*;
import android.util.Log;
import android.view.View;
import android.widget.*;
public class PhoneBook extends Activity{
ProductDBHelper mHelper;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.productlist);
mHelper = new ProductDBHelper(this);
Cursor cursor;
SQLiteDatabase db = mHelper.getWritableDatabase();
cursor = db.rawQuery("SELECT * FROM phonebook", null);
startManagingCursor(cursor);
//String[] Query = new String[] {"name","cellphon","email","photos"};
String[] Query = new String[] {"name","cellphon"};
int[] text = new int[] { android.R.id.text1,android.R.id.text2};
SimpleCursorAdapter Adapter = null;
Adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2,
cursor,Query,
text);
ListView list = (ListView)findViewById(R.id.list);
//list = (ListView)findViewById(R.id.history);
list.setAdapter(Adapter);
list.setOnItemClickListener(mItemClickListener);
}
// 리스트 아이템 클릭리스너
AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener() {
// (어댑터 뷰, 항목 뷰, 항목의 위치, 항목의 고유 ID)
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
//String mes;
//mes = "Select Item = " + Items.get(position);
//Toast.makeText(ListAddDel.this, mes, Toast.LENGTH_SHORT).show();
Log.i("test", "아이템 클릭2");
}
};
}
class ProductDBHelper extends SQLiteOpenHelper {
public ProductDBHelper(Context context) {
super(context, "Android.sqlite", null, 1);
}
public void onCreate(SQLiteDatabase db) {
/*db.execSQL("CREATE TABLE product ( _id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"name TEXT, price INTEGER);");
*/
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS product");
onCreate(db);
}
}
제가 여기서 디비를 불러왔는데요 디비를 프로젝트 안에 assets 폴더 안에 디비파일을 넣고 그걸 불러오는 식으로 할순없나요
지금 상태에서는 DDMS에서 컨트롤 해줘야 디비를 불러오거든요
어떻게 해야되나요
package AHNdroid.PhonBook;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.*;
import android.content.*;
import android.content.res.AssetManager;
import android.database.*;
import android.database.sqlite.*;
import android.os.*;
import android.util.Log;
import android.view.View;
import android.widget.*;
public class PhonBook extends Activity{
ProductDBHelper mHelper;
//onair.db 파일을 복사
public void setDB() {
File outfile = new File("/data/data/AHNdroid.PhonBook/databases/Android.sqlite"); //--폰에 위치할 경로
AssetManager assetManager = getResources().getAssets();
InputStream is = null;
FileOutputStream fo = null;
long filesize = 0;
//Intent i = getIntent();
//long mAssetDbDate = Long.parseLong(i.getStringExtra("mDBLASTUP"));
//long mPhoneDbDate = outfile.lastModified();
//showToast("mAssetDbDate -------------- " + mAssetDbDate);
//showToast("mPhoneDbDate -------------- " + mPhoneDbDate);
try {
is = assetManager.open("assets/Android.sqlite", AssetManager.ACCESS_BUFFER);// --asset 폴더 및 복사할 DB 지정
filesize = is.available(); //--사이즈 검증
// 파일이 없거나 패키지 폴더에 설치된 DB파일이 포함된 DB파일 보다 크기가 같지않을 경우 DB파일을 덮어 쓴다.
if(outfile.length() != filesize || !outfile.exists()){
byte[] tempdata = new byte[(int) filesize];
is.read(tempdata);
is.close();
outfile.createNewFile();
fo = new FileOutputStream(outfile);
fo.write(tempdata);
fo.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.productlist);
mHelper = new ProductDBHelper(this);
Cursor cursor;
SQLiteDatabase db = mHelper.getWritableDatabase();
cursor = db.rawQuery("SELECT * FROM phonbook", null);
startManagingCursor(cursor);
//String[] Query = new String[] {"name","cellphon","email","photos"};
String[] Query = new String[] {"name","cellphon"};
int[] text = new int[] { android.R.id.text1,android.R.id.text2};
SimpleCursorAdapter Adapter = null;
Adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2,
cursor,Query,
text);
ListView list = (ListView)findViewById(R.id.list);
//list = (ListView)findViewById(R.id.history);
list.setAdapter(Adapter);
list.setOnItemClickListener(mItemClickListener);
}
// 리스트 아이템 클릭리스너
AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener() {
// (어댑터 뷰, 항목 뷰, 항목의 위치, 항목의 고유 ID)
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//String Ames;
//mes = "Select Item = " + Items.get(position);
//Toast.makeText(ListAddDel.this, mes, Toast.LENGTH_SHORT).show();
Log.i("test", "아이템 클릭2");
}
};
}
class ProductDBHelper extends SQLiteOpenHelper {
public ProductDBHelper(Context context) {
super(context, "Android.sqlite", null, 1);
}
public void onCreate(SQLiteDatabase db) {
/*db.execSQL("CREATE TABLE product ( _id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"name TEXT, price INTEGER);");
db.execSQL("INSERT INTO product VALUES (null, '오징어 땅콩', 900);");
db.execSQL("INSERT INTO product VALUES (null, '농심 포테이토 칩', 2000);");
db.execSQL("INSERT INTO product VALUES (null, '로보트 태권 V', 1000);");
db.execSQL("INSERT INTO product VALUES (null, '꼬마 자동차 붕붕', 1500);");
db.execSQL("INSERT INTO product VALUES (null, '윈도우즈 API 정복', 32000);");
db.execSQL("INSERT INTO product VALUES (null, '롯데 인벤스 아파트', 190000000);");
db.execSQL("INSERT INTO product VALUES (null, '88 라이트', 1900);");
db.execSQL("INSERT INTO product VALUES (null, '프라이드 1.6 CVVT 골드', 8900000);");
db.execSQL("INSERT INTO product VALUES (null, '캐리비안 베이 입장권', 25000);");
*/
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS product");
onCreate(db);
}
}
이렇게 했는데 오류 떠욤 ㅠㅠ
디비쪽은 까막눈이라 어떻게 해야될지 모르겠네요 ㅠ




제가 사용했던 방식입니다...도움되셨으면 좋겠습니다
//onair.db 파일을 복사
public void setDB() {
File outfile = new File("/data/data/com.android.onair/databases/onair.db"); --폰에 위치할 경로
AssetManager assetManager = getResources().getAssets();
InputStream is = null;
FileOutputStream fo = null;
long filesize = 0;
//Intent i = getIntent();
//long mAssetDbDate = Long.parseLong(i.getStringExtra("mDBLASTUP"));
//long mPhoneDbDate = outfile.lastModified();
//showToast("mAssetDbDate -------------- " + mAssetDbDate);
//showToast("mPhoneDbDate -------------- " + mPhoneDbDate);
try {
is = assetManager.open("database/onair.db", AssetManager.ACCESS_BUFFER); --asset 폴더 및 복사할 DB 지정
filesize = is.available(); --사이즈 검증
// 파일이 없거나 패키지 폴더에 설치된 DB파일이 포함된 DB파일 보다 크기가 같지않을 경우 DB파일을 덮어 쓴다.
if(outfile.length() != filesize || !outfile.exists()){
byte[] tempdata = new byte[(int) filesize];
is.read(tempdata);
is.close();
outfile.createNewFile();
fo = new FileOutputStream(outfile);
fo.write(tempdata);
fo.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
이상입니다.