0

有人可以帮忙吗。我一直在创建一个应用程序,到目前为止,我的数据已成功保存到数据库中,但是 not null 似乎根本不起作用,因为我可以提交空白值并且它们可以很好地保存到数据库中。我还收到一个数据库错误“close() 从未在数据库上显式调用”。

package com.example.tasksystemapp;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashSet;
import java.util.Set;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDatabaseHelper extends SQLiteOpenHelper {

//singleton/ single instance reference of database instance
private static MyDatabaseHelper _dbHelper;

//The Android's default system path of your application database.
//private static String DB_PATH = "/data/data/YOUR_PACKAGE/";

private static String DB_PATH = "/data/data/com.example.tasksystemapp/";

//database name
private static String DB_NAME = "tasksystem.db";

//database version
private static int VERSION=1;
private final Context _context;


public MyDatabaseHelper(Context context)
{
    super(context, DB_NAME, null, VERSION);
    this._context = context;

}
public static MyDatabaseHelper getInstance(Context context)
{
    if(_dbHelper == null)
    {
        _dbHelper = new MyDatabaseHelper(context);
    }
    return _dbHelper;
}


public String createDataBase() throws IOException
{
    boolean dbExist = checkDataBase();
    if(dbExist)
    {
        //do nothing - database already exist
    }else{

        this.getReadableDatabase();

        try {

            copyDataBase();

        } catch (IOException e) {

            return (e.getMessage());

        }
    }

    return "database copied";

}

private boolean checkDataBase()
{
    SQLiteDatabase checkDB = null;

    try
    {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    }catch(SQLiteException e)
    {
        //database does't exist yet.
    }
    if(checkDB != null)
    {
        checkDB.close();
    }

    return checkDB != null ? true : false;
}
public void copyDataBase() throws IOException
{
    //Open your local db as the input stream
    InputStream myInput = _context.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];
    int length;
    while ((length = myInput.read(buffer))>0)
    {
        myOutput.write(buffer, 0, length);
    }
    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();
}


@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL("CREATE TABLE Contact (_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
                                      "lastname TEXT NOT NULL, firstname TEXT NOT NULL, email TEXT, " +
                                      "phone TEXT, address TEXT, city TEXT, postcode TEXT);");

    db.execSQL("CREATE TABLE Action (_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
                                        "Actiondescription TEXT);");

    db.execSQL("CREATE TABLE Autoreminder (_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
                                            "Autoreminderdescription TEXT, Autoremindate TEXT, Autoremintime TEXT);");

    db.execSQL("CREATE TABLE Priority (_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
                                        "Actionid INTEGER);");

    db.execSQL("CREATE TABLE Task (_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
                                        "Description TEXT NOT NULL, Priority TEXT, Duedate TEXT, Duetime TEXT " +
                                        "Completed INTEGER);");

    db.execSQL("CREATE TABLE User (_id TEXT PRIMARY KEY NOT NULL, " +
                                        "password TEXT NOT NULL, name TEXT NOT NULL, phoneno INTEGER);");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


}

}

4

0 回答 0