Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法将数据插入Android中的SQLite数据库_Android_Sqlite_Android Sqlite_Sql Insert - Fatal编程技术网

无法将数据插入Android中的SQLite数据库

无法将数据插入Android中的SQLite数据库,android,sqlite,android-sqlite,sql-insert,Android,Sqlite,Android Sqlite,Sql Insert,我有SQlite数据库,我保存在我的项目的资产文件夹中。 从资产文件夹中,我正在将sqlite db复制到data\data\packagename\MYSHOES.sqlite。 我已成功从此数据库检索数据,但当 我正在尝试插入数据,但它没有做任何事情。没有错误消息。 下面是我的代码 public class Shoedb extends SQLiteOpenHelper { private SQLiteDatabase myDataBase; private final Context my

我有SQlite数据库,我保存在我的项目的资产文件夹中。 从资产文件夹中,我正在将sqlite db复制到data\data\packagename\MYSHOES.sqlite。 我已成功从此数据库检索数据,但当 我正在尝试插入数据,但它没有做任何事情。没有错误消息。 下面是我的代码

public class Shoedb extends SQLiteOpenHelper {
private SQLiteDatabase myDataBase;
private final Context myContext;

private static String DB_DIR = "/data/data/com.customlistview/";
private static String DB_NAME = "MYSHOES.sqlite";
private static String DB_PATH = DB_DIR + DB_NAME;
boolean dbfnd;
Cursor chk;
private String TAG = "Database Helper";

public Shoedb(Context context) {

    super(context, Shoedb.DB_NAME, null, 1);
    this.myContext = context;
    // DB_PATH=myContext.getDatabasePath(DB_NAME).getAbsolutePath();
    System.out.println("My DataBase Path: " + DB_PATH);
    try {
        copyDataBase();
        openDataBase();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void createDataBase() throws IOException {

    Log.e("createDataBase1", "+++++++++++++");
    boolean dbExist = checkDataBase();

    if (dbExist) {
        Log.e("createDataBase2", "++++++ database already exist");
        // do nothing - database already exist
    } else {

        // this.getReadableDatabase();

        try {

            copyDataBase();

        } catch (IOException e) {

            throw new Error("Error copying database");

        }
    }

}

private boolean checkDataBase() {

    SQLiteDatabase checkDB = null;

    try {
        String myPath = DB_PATH;
        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;

}

private void copyDataBase() throws IOException {

    // Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);

    // Path to the just created empty db
    String outFileName = DB_PATH;

    // 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();

}

public static void copyFile(InputStream fromFile, OutputStream toFile)
        throws IOException {
    // transfer bytes from the inputfile to the outputfile
    System.out.println("In copying.....");
    byte[] buffer = new byte[1024];
    int length;

    try {
        while ((length = fromFile.read(buffer)) > 0) {
            toFile.write(buffer, 0, length);
            System.out.println("Reading & writing the file....");
        }
    } catch (Exception e) {
        System.out.println("Error in copy1 file :" + e.toString());
    }
    // Close the streams
    finally {
        try {
            if (toFile != null) {
                try {
                    toFile.flush();
                } finally {
                    toFile.close();
                }
            }
        } catch (Exception e) {
            System.out.println("Error in copy2 file :" + e.toString());
        } finally {
            if (fromFile != null) {
                fromFile.close();
                System.out.println("File copied successfully.....");
            }
        }
    }
}

public static void copyFile(String fromFile, String toFile)
        throws IOException {
    copyFile(new FileInputStream(fromFile), new FileOutputStream(toFile));
}

// Open the database
public void openDataBase() {

    String myPath = DB_PATH;
    try {

        myDataBase = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE);
        System.out.println(myDataBase.toString()
                + " Database found....................!!!!!!!");
        dbfnd = true;
    } catch (Exception e) {
        System.out.println("Database not found....................!!!!!!!");
        // TODO: handle exception
        dbfnd = false;
    }
}

public SQLiteDatabase getReadableDatabase() {
    myDataBase = SQLiteDatabase.openDatabase(DB_PATH, null,

    SQLiteDatabase.OPEN_READONLY);
    return myDataBase;
}

public SQLiteDatabase getWritableDatabase() {
    myDataBase = SQLiteDatabase.openDatabase(DB_PATH, null,

    SQLiteDatabase.OPEN_READWRITE);

    return myDataBase;
}

@Override
public synchronized void close() {

    if (myDataBase != null)
        myDataBase.close();
    super.close();

}

@Override
public void onCreate(SQLiteDatabase db) {

}

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

}

public Cursor get_Data(String selCat) {
    chk = null;

    try {

        chk = myDataBase.rawQuery(
                "select Image,Price,Category from shoe_info where Category='"
                        + selCat + "'", null);

    } catch (Exception e) {
        Log.e(TAG, "Error in getshoeinfo");
        e.printStackTrace();
        return null;
    }
    return chk;

}

public Cursor get_DataImage(String pImage) {
    // TODO Auto-generated method stub
    chk = null;

    try {

        chk = myDataBase.rawQuery(
                "select Image,Price,Category from shoe_info where Image='"
                        + pImage + "'", null);

    } catch (Exception e) {
        Log.e(TAG, "Error in getshoeinfo");
        e.printStackTrace();
        return null;
    }
    return chk;
}
     //This code is not working
public void put_DataImage(String pImage, String price, String id) {
    // TODO Auto-generated method stub
    chk = null;

    try {
        System.out
                .println("insert into shoe_info(Image,Price,Category) values('"
                        + pImage + "','" + price + "','" + id + "');");
        myDataBase
                .execSQL("insert into shoe_info(Image,Price,Category) values('"
                        + pImage + "','" + price + "','" + id + "');");

    } catch (Exception e) {
        Log.e(TAG, "Error in getshoeinfo");
        e.printStackTrace();
    }
}

public void put_DataToCart(String image, String price, String pCat) {
    // TODO Auto-generated method stub
    Log.i(TAG, "Insert to mycarttttttttttttttttttt");
    try {
        System.out
                .println("insert into mycart (Image,Price,category) values('"
                        + image + "','" + price + "','" + pCat + "');");

        myDataBase
                .execSQL("insert into mycart(Image,Price,category) values('"
                        + image + "','" + price + "','" + pCat + "')");

    } catch (Exception e) {
        Log.e(TAG, "Error in put_DataToCart");
        e.printStackTrace();
    }
}

public Cursor get_DataFromCart() {
    chk = null;

    try {
        System.out.println("select Image,Price,category from mycart");
        chk = myDataBase.rawQuery(
                "select Image,Price,category from mycart", null);

    } catch (Exception e) {
        Log.e(TAG, "Error in getshoeinfo");
        e.printStackTrace();
        return null;
    }
    return chk;

}
}尝试使用

SQLiteDatabase.OPEN\u READWRITE而不是SQLiteDatabase.OPEN\u READONLY


在checkDataBase()中打开与数据库的连接。当前与数据库的连接是只读的…

您正在调用哪种方法来插入数据…如果可能,请提供异常堆栈跟踪(如果有的话)…这将有所帮助