实际上我正在开发应用程序运行时的一个应用程序 第一次它会创建一个txt文件,如果文件已经存在,那么它只会在文件中写入 但我不知道如何以编程方式检查 文件是否存在。我试过了,
try
{
FPartyCreation = new File ("/data/data/com.MediExcel/files/","dbPartyCreation.dat");
if (FPartyCreation.exists())
{
writeOnFile();
}
else
{
FPartyCreation.createNewFile();
writeOnFile();
}
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
但它会产生以下错误,并且文件也不会在同一路径上创建。
07-06 01:39:26.183: E/AndroidRuntime(23107): FATAL EXCEPTION: main
07-06 01:39:26.183: E/AndroidRuntime(23107): java.lang.NullPointerException
07-06 01:39:26.183: E/AndroidRuntime(23107): at com.MediExcel.AddMethodPartyCreation.getMethod(AddMethodPartyCreation.java:28)
07-06 01:39:26.183: E/AndroidRuntime(23107): at com.MediExcel.PartyCreationActivity$1.onClick(PartyCreationActivity.java:64)
07-06 01:39:26.183: E/AndroidRuntime(23107): at android.view.View.performClick(View.java:3511)
07-06 01:39:26.183: E/AndroidRuntime(23107): at android.view.View$PerformClick.run(View.java:14105)
07-06 01:39:26.183: E/AndroidRuntime(23107): at android.os.Handler.handleCallback(Handler.java:605)
07-06 01:39:26.183: E/AndroidRuntime(23107): at android.os.Handler.dispatchMessage(Handler.java:92)
07-06 01:39:26.183: E/AndroidRuntime(23107): at android.os.Looper.loop(Looper.java:137)
07-06 01:39:26.183: E/AndroidRuntime(23107): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-06 01:39:26.183: E/AndroidRuntime(23107): at java.lang.reflect.Method.invokeNative(Native Method)
07-06 01:39:26.183: E/AndroidRuntime(23107): at java.lang.reflect.Method.invoke(Method.java:511)
07-06 01:39:26.183: E/AndroidRuntime(23107): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-06 01:39:26.183: E/AndroidRuntime(23107): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-06 01:39:26.183: E/AndroidRuntime(23107): at dalvik.system.NativeStart.main(Native Method)
请帮帮我
谢谢
答案 0 :(得分:1)
创建SQLite数据库 -
public class DatabaseHelper extends SQLiteOpenHelper {
static final String dbName="demoDB";
static final String employeeTable="Employees";
static final String colID="EmployeeID";
static final String colName="EmployeeName";
static final String colAge="Age";
static final String colDept="Dept";
static final String deptTable="Dept";
static final String colDeptID="DeptID";
static final String colDeptName="DeptName";
static final String viewEmps="ViewEmps";
创建数据库
public void onCreate(SQLiteDatabase db)
{
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE "+deptTable+" ("+colDeptID+ " INTEGER PRIMARY KEY , "+
colDeptName+ " TEXT)");
db.execSQL("CREATE TABLE "+employeeTable+"
("+colID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
colName+" TEXT, "+colAge+" Integer, "+colDept+"
INTEGER NOT NULL ,FOREIGN KEY ("+colDept+") REFERENCES
"+deptTable+" ("+colDeptID+"));");
db.execSQL("CREATE TRIGGER fk_empdept_deptid " +
" BEFORE INSERT "+
" ON "+employeeTable+
" FOR EACH ROW BEGIN"+
" SELECT CASE WHEN ((SELECT "+colDeptID+" FROM "+deptTable+"
WHERE "+colDeptID+"=new."+colDept+" ) IS NULL)"+
" THEN RAISE (ABORT,'Foreign Key Violation') END;"+
" END;");
db.execSQL("CREATE VIEW "+viewEmps+
" AS SELECT "+employeeTable+"."+colID+" AS _id,"+
" "+employeeTable+"."+colName+","+
" "+employeeTable+"."+colAge+","+
" "+deptTable+"."+colDeptName+""+
" FROM "+employeeTable+" JOIN "+deptTable+
" ON "+employeeTable+"."+colDept+" ="+deptTable+"."+colDeptID
);
//Inserts pre-defined departments
InsertDepts(db);
}
查看此Using-SQLite-Database-with-Android博客。希望这个博客足以满足您的疑问。