我想把数据存到SD卡中,代码如下package zm.Data;import java.io.File;
import java.io.IOException;import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;public class Db {
private Context ct;
private SQLiteDatabase db=null;
private File path = new File("/sdcard/zm");// 创建目录
private File f = new File("/sdcard/zm/data.db");// 创建文件
public Db(Context context){
ct=context;
} public Db openDB()throws SQLException {
db=SQLiteDatabase.openOrCreateDatabase(f, null);
return this;
}
public void createTable(){
db.execSQL("Create table tab_signal(time String,longitude text,latitude text,shownetworktype text,signalstrength text)");
db.execSQL("Create table tab_avedata(time String,avesignal text)");
db.execSQL("Create table tab_call(date String,type text,number text,duration text,isread text,isnew text)");
db.execSQL("Create table tab_traffic(time String,app text,traffic text)");
}
public void closeDB(){
db.close();
}
public boolean addData(String t,double lon,double lat,String shownetworktype,int signalstrength){
boolean result=true;
try{
String sql=String.format("insert into tab_signal(time,longitude,latitude,shownetworktype,signalstrength)values('%s',%f,%f,'%s',%d)", t,lon,lat,shownetworktype,signalstrength);
db.execSQL(sql);
result=true;
System.out.println("数据库-----保存数据成功");
Toast.makeText(ct, "保存数据成功", Toast.LENGTH_SHORT).show();
}catch(Exception e)
{
result=false;
Toast.makeText(ct, "保存数据失败"+e.getMessage(), Toast.LENGTH_SHORT).show();
}
return result;
}
public boolean addAveData(String t,int avesignal){
boolean result=true;
try{
String sql01=String.format("insert into tab_avedata(time,avesignal)values('%s',%d)",t,avesignal);
db.execSQL(sql01);
result=true;
System.out.println("数据库-----平均数保存成功");
}catch(Exception e)
{
System.out.println("数据库-----平均数保存失败");
result=false;
}
return result;
}
public boolean addCallData(String date,int type,String number,String duration,String isread,String isnew){
boolean result=true;
try{
String sql=String.format("insert into tab_call(date,type,number,duration,isread,isnew)values('%s',%d,'%s','%s','%s','%s')", date,type,number,duration,isread,isnew);
db.execSQL(sql);
result=true;
System.out.println("数据库-----保存通话记录数据成功");
}catch(Exception e){
System.out.println("数据库-----保存通话记录数据失败");
result=false;
}
return result;
}
public boolean addTrafficData(String time,String app,long traffic){
boolean result=true;
try{
String sql=String.format("insert into tab_call(time,app,traffic)values('%s','%s',%d)",time,app,traffic);
db.execSQL(sql);
result=true;
System.out.println("数据库-----保存流量数据成功");
}catch(Exception e){
System.out.println("数据库-----保存流量数据失败");
result=false;
}
return result;
}
}
可是,一运行,则报错,db=SQLiteDatabase.openOrCreateDatabase(f, null);这句报错,导致数据库不能打开,不知道哪里出问题了,求高人指点
import java.io.IOException;import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;public class Db {
private Context ct;
private SQLiteDatabase db=null;
private File path = new File("/sdcard/zm");// 创建目录
private File f = new File("/sdcard/zm/data.db");// 创建文件
public Db(Context context){
ct=context;
} public Db openDB()throws SQLException {
db=SQLiteDatabase.openOrCreateDatabase(f, null);
return this;
}
public void createTable(){
db.execSQL("Create table tab_signal(time String,longitude text,latitude text,shownetworktype text,signalstrength text)");
db.execSQL("Create table tab_avedata(time String,avesignal text)");
db.execSQL("Create table tab_call(date String,type text,number text,duration text,isread text,isnew text)");
db.execSQL("Create table tab_traffic(time String,app text,traffic text)");
}
public void closeDB(){
db.close();
}
public boolean addData(String t,double lon,double lat,String shownetworktype,int signalstrength){
boolean result=true;
try{
String sql=String.format("insert into tab_signal(time,longitude,latitude,shownetworktype,signalstrength)values('%s',%f,%f,'%s',%d)", t,lon,lat,shownetworktype,signalstrength);
db.execSQL(sql);
result=true;
System.out.println("数据库-----保存数据成功");
Toast.makeText(ct, "保存数据成功", Toast.LENGTH_SHORT).show();
}catch(Exception e)
{
result=false;
Toast.makeText(ct, "保存数据失败"+e.getMessage(), Toast.LENGTH_SHORT).show();
}
return result;
}
public boolean addAveData(String t,int avesignal){
boolean result=true;
try{
String sql01=String.format("insert into tab_avedata(time,avesignal)values('%s',%d)",t,avesignal);
db.execSQL(sql01);
result=true;
System.out.println("数据库-----平均数保存成功");
}catch(Exception e)
{
System.out.println("数据库-----平均数保存失败");
result=false;
}
return result;
}
public boolean addCallData(String date,int type,String number,String duration,String isread,String isnew){
boolean result=true;
try{
String sql=String.format("insert into tab_call(date,type,number,duration,isread,isnew)values('%s',%d,'%s','%s','%s','%s')", date,type,number,duration,isread,isnew);
db.execSQL(sql);
result=true;
System.out.println("数据库-----保存通话记录数据成功");
}catch(Exception e){
System.out.println("数据库-----保存通话记录数据失败");
result=false;
}
return result;
}
public boolean addTrafficData(String time,String app,long traffic){
boolean result=true;
try{
String sql=String.format("insert into tab_call(time,app,traffic)values('%s','%s',%d)",time,app,traffic);
db.execSQL(sql);
result=true;
System.out.println("数据库-----保存流量数据成功");
}catch(Exception e){
System.out.println("数据库-----保存流量数据失败");
result=false;
}
return result;
}
}
可是,一运行,则报错,db=SQLiteDatabase.openOrCreateDatabase(f, null);这句报错,导致数据库不能打开,不知道哪里出问题了,求高人指点
private File path = new File("/sdcard/zm");// 创建目录
private File f = new File("/sdcard/zm/data.db");// 创建文件
这句应该改成这样的吧:
db=SQLiteDatabase.openOrCreateDatabase(f.getPath(), null);试试!