@Override public void onClick(View v) { queryDB();
} }); } public void createDB() { DBManager dbm = new DBManager(this); dbm.openDatabase(); dbm.closeDatabase(); } public void queryDB() { String sql = "select * from Employees";
DBManager dbm = new DBManager(this); SQLiteDatabase db = dbm.openDatabase(); Cursor cur = db.rawQuery(sql, null);
while (cur.moveToNext()) { //String id = cur.getColumnName(1); //float la = cur.ge; //float id = cur.getColumnName(1); int id = cur.getInt(1); Log.i("latitude", "id:"+id); } cur.close(); db.close(); }
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }} 数据库只有一个表格:
非常感谢您! 我通过您的代码 写入了。但是我想查询里面的数据,locat结果说,没有我想查询的表格,我不知道什么原因,麻烦你解答一下: 代码如下: public class DBManager { private Context context; public final String DB_PATH2 = android.os.Environment.getExternalStorageDirectory().getAbsolutePath()+"/tianke"; private final String DB_NAME = "MyDatabase.db"; private SQLiteDatabase database; private final int BUFFER_SIZE = 1024; DBManager(Context context) { this.context = context; }// public void openDatabase() { // this.database = this.openDatabase(DB_PATH2 + "/" + DB_NAME); // System.out.println("DB_PATH2:"+ DB_PATH2); // }public SQLiteDatabase openDatabase() { File dbFile = new File(DB_PATH2+"/MyDatabase.db"); if(!dbFile.exists()){ //InputStream is = this.context.getResources().openRawResource(R.raw.); try { //创建目录 File pmsPaht = new File(DB_PATH2); Log.i("pmsPaht", "pmsPaht: "+pmsPaht.getPath()); pmsPaht.mkdirs(); AssetManager am = this.context.getAssets(); InputStream is = am.open(DB_NAME); FileOutputStream fos = new FileOutputStream(dbFile); byte[] buffer = new byte[BUFFER_SIZE]; int count = 0; while ((count = is.read(buffer)) > 0) { fos.write(buffer, 0, count); } fos.flush(); fos.close(); is.close(); am.close(); }catch (IOException e) { e.printStackTrace(); } } database = SQLiteDatabase.openOrCreateDatabase(dbFile, null); return database; } //http://jalhaskazakhlike.lofter.com/post/3f320a_15df9b3 //http://www.cnblogs.com/xiaowenji/archive/2011/01/03/1925014.html public void closeDatabase() { if (database!=null){ this.database.close(); } }} public class MainActivity extends Activity {@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button create = (Button) findViewById(R.id.button1); Button query = (Button) findViewById(R.id.button2); create.setOnClickListener(new OnClickListener() {@Override public void onClick(View v) {createDB(); } }); query.setOnClickListener(new OnClickListener() {@Override public void onClick(View v) { queryDB();} }); } public void createDB() { DBManager dbm = new DBManager(this); dbm.openDatabase(); dbm.closeDatabase(); } public void queryDB() { String sql = "select * from Employees";
DBManager dbm = new DBManager(this); SQLiteDatabase db = dbm.openDatabase(); Cursor cur = db.rawQuery(sql, null);
while (cur.moveToNext()) { //String id = cur.getColumnName(1); //float la = cur.ge; //float id = cur.getColumnName(1); int id = cur.getInt(1); Log.i("latitude", "id:"+id); } cur.close(); db.close(); }
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }} 数据库只有一个表格:
下面这个代码可以用/**
* 从Apk本地Assets复制文件到指定文件夹
*
* @param context
* @param filepath
* @param fileName
* @return
*/
public static boolean copyFileFromAssets(Context context, String filepath,
String fileName) {
boolean result = false;
try {
// 检查 SQLite 数据库文件是否存在
if ((new File(filepath + fileName)).exists() == false) {
// 如 SQLite 数据库文件不存在,再检查一下 database 目录是否存在
File f = new File(filepath);
// 如 database 目录不存在,新建该目录
if (!f.exists()) {
f.mkdir();
}
try {
// 得到 assets 目录下我们实现准备好的 SQLite 数据库作为输入流
InputStream is = context.getAssets().open(fileName);
// 输出流
OutputStream os = new FileOutputStream(filepath + fileName);
// 文件写入
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
// 关闭文件流
os.flush();
os.close();
is.close();
result = true;
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
// TODO: handle exception
}
return result;
}
还记得加上读写sd卡的权限
非常感谢您! 我通过您的代码 写入了。但是我想查询里面的数据,locat结果说,没有我想查询的表格,我不知道什么原因,麻烦你解答一下:
代码如下:
public class DBManager {
private Context context;
public final String DB_PATH2 = android.os.Environment.getExternalStorageDirectory().getAbsolutePath()+"/tianke";
private final String DB_NAME = "MyDatabase.db";
private SQLiteDatabase database;
private final int BUFFER_SIZE = 1024;
DBManager(Context context) {
this.context = context;
}
// public void openDatabase() {
// this.database = this.openDatabase(DB_PATH2 + "/" + DB_NAME);
// System.out.println("DB_PATH2:"+ DB_PATH2);
// } public SQLiteDatabase openDatabase() {
File dbFile = new File(DB_PATH2+"/MyDatabase.db");
if(!dbFile.exists()){
//InputStream is = this.context.getResources().openRawResource(R.raw.);
try {
//创建目录
File pmsPaht = new File(DB_PATH2);
Log.i("pmsPaht", "pmsPaht: "+pmsPaht.getPath());
pmsPaht.mkdirs();
AssetManager am = this.context.getAssets();
InputStream is = am.open(DB_NAME);
FileOutputStream fos = new FileOutputStream(dbFile);
byte[] buffer = new byte[BUFFER_SIZE];
int count = 0;
while ((count = is.read(buffer)) > 0) {
fos.write(buffer, 0, count);
}
fos.flush();
fos.close();
is.close();
am.close();
}catch (IOException e) {
e.printStackTrace();
}
}
database = SQLiteDatabase.openOrCreateDatabase(dbFile, null);
return database;
}
//http://jalhaskazakhlike.lofter.com/post/3f320a_15df9b3
//http://www.cnblogs.com/xiaowenji/archive/2011/01/03/1925014.html
public void closeDatabase() {
if (database!=null){
this.database.close();
}
}}
public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button create = (Button) findViewById(R.id.button1);
Button query = (Button) findViewById(R.id.button2);
create.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
createDB();
}
});
query.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
queryDB();
}
});
}
public void createDB()
{
DBManager dbm = new DBManager(this);
dbm.openDatabase();
dbm.closeDatabase();
}
public void queryDB()
{
String sql = "select * from Employees";
DBManager dbm = new DBManager(this);
SQLiteDatabase db = dbm.openDatabase();
Cursor cur = db.rawQuery(sql, null);
while (cur.moveToNext())
{
//String id = cur.getColumnName(1);
//float la = cur.ge;
//float id = cur.getColumnName(1);
int id = cur.getInt(1);
Log.i("latitude", "id:"+id);
}
cur.close();
db.close();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}}
数据库只有一个表格:
代码如下:
public class DBManager {
private Context context;
public final String DB_PATH2 = android.os.Environment.getExternalStorageDirectory().getAbsolutePath()+"/tianke";
private final String DB_NAME = "MyDatabase.db";
private SQLiteDatabase database;
private final int BUFFER_SIZE = 1024;
DBManager(Context context) {
this.context = context;
}// public void openDatabase() {
// this.database = this.openDatabase(DB_PATH2 + "/" + DB_NAME);
// System.out.println("DB_PATH2:"+ DB_PATH2);
// }public SQLiteDatabase openDatabase() {
File dbFile = new File(DB_PATH2+"/MyDatabase.db");
if(!dbFile.exists()){
//InputStream is = this.context.getResources().openRawResource(R.raw.);
try {
//创建目录
File pmsPaht = new File(DB_PATH2);
Log.i("pmsPaht", "pmsPaht: "+pmsPaht.getPath());
pmsPaht.mkdirs();
AssetManager am = this.context.getAssets();
InputStream is = am.open(DB_NAME);
FileOutputStream fos = new FileOutputStream(dbFile);
byte[] buffer = new byte[BUFFER_SIZE];
int count = 0;
while ((count = is.read(buffer)) > 0) {
fos.write(buffer, 0, count);
}
fos.flush();
fos.close();
is.close();
am.close();
}catch (IOException e) {
e.printStackTrace();
}
}
database = SQLiteDatabase.openOrCreateDatabase(dbFile, null);
return database;
}
//http://jalhaskazakhlike.lofter.com/post/3f320a_15df9b3
//http://www.cnblogs.com/xiaowenji/archive/2011/01/03/1925014.html
public void closeDatabase() {
if (database!=null){
this.database.close();
}
}}
public class MainActivity extends Activity {@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button create = (Button) findViewById(R.id.button1);
Button query = (Button) findViewById(R.id.button2);
create.setOnClickListener(new OnClickListener() {@Override
public void onClick(View v) {createDB();
}
});
query.setOnClickListener(new OnClickListener() {@Override
public void onClick(View v) {
queryDB();}
});
}
public void createDB()
{
DBManager dbm = new DBManager(this);
dbm.openDatabase();
dbm.closeDatabase();
}
public void queryDB()
{
String sql = "select * from Employees";
DBManager dbm = new DBManager(this);
SQLiteDatabase db = dbm.openDatabase();
Cursor cur = db.rawQuery(sql, null);
while (cur.moveToNext())
{
//String id = cur.getColumnName(1);
//float la = cur.ge;
//float id = cur.getColumnName(1);
int id = cur.getInt(1);
Log.i("latitude", "id:"+id);
}
cur.close();
db.close();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}}
数据库只有一个表格:
我是把数据库放到手机中sdcard中。出现了无法读取问题,但是数据库已经写进去了啊。
谢谢版主啊!能不能给点code提示一下,或者你可以下载一下我的code,看看什么问题,替我修改一下,我这是第一次接触andorid Sqlite。实在是麻烦你了!我的源代码文件可以在下面链接下载:
我的整个文件源代码可以从这个链接下载:
http://download.csdn.net/detail/tianke0711/8058783