在 sqlite 中保存文本文件 android
save text file in sqlite android
我想在 sqlite android 中保存文本文件,但它在第 long result = db.insert(TABLE_NAME,null,cv);
行给出错误 android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed:
。
@Override
public void onCreate(SQLiteDatabase db) {
String create_table = "CREATE TABLE " + TABLE_NAME + "( "
+ "ID INTEGER PRIMARY KEY ,"
+ POSITION + " TEXT NOT NULL, "
+ TXT_FILE + " BLOB NOT NULL, "
+ _ID + " TEXT NOT NULL)";
db.execSQL(create_table);
}
public boolean add_txt_file(String id, byte[] txt_file) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(_ID,id);
cv.put(TXT_FILE,txt_file);
long result = db.insert(TABLE_NAME,null,cv);
if (result == -1){
return false;
} else {
return true;
}
}
boolean save_txt_file = db.add_txt_file(id,
readFile(path));
if (save_txt_file){
Toast.makeText(this, "saved successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "failed", Toast.LENGTH_SHORT).show();
}
private byte[] readFile(String file) {
ByteArrayOutputStream bos = null;
try {
File f = new File(Environment.getExternalStorageDirectory(),file);
FileInputStream fis = new FileInputStream(f);
byte[] buffer = new byte[1024];
bos = new ByteArrayOutputStream();
for (int len; (len = fis.read(buffer)) != -1;) {
bos.write(buffer, 0, len);
}
} catch (FileNotFoundException e) {
Log.e("TAG","error " + e.getMessage());
} catch (IOException e2) {
System.err.println(e2.getMessage());
}
return bos != null ? bos.toByteArray() : null;
}
path
在日志 Download/Link_on.txt
和 readFile(path)
returns [49, 10, 48, 48, 58, 48......但它仍然无法将文本文件保存在数据库中。感谢您的帮助。
在 table 的 CREATE
语句中,您已将列 POSITION
定义为 NOT NULL
,但在方法 add_txt_file()
中您没有提供一个值。
添加如下行:
cv.put(POSITION, "some value");
我想在 sqlite android 中保存文本文件,但它在第 long result = db.insert(TABLE_NAME,null,cv);
行给出错误 android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed:
。
@Override
public void onCreate(SQLiteDatabase db) {
String create_table = "CREATE TABLE " + TABLE_NAME + "( "
+ "ID INTEGER PRIMARY KEY ,"
+ POSITION + " TEXT NOT NULL, "
+ TXT_FILE + " BLOB NOT NULL, "
+ _ID + " TEXT NOT NULL)";
db.execSQL(create_table);
}
public boolean add_txt_file(String id, byte[] txt_file) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(_ID,id);
cv.put(TXT_FILE,txt_file);
long result = db.insert(TABLE_NAME,null,cv);
if (result == -1){
return false;
} else {
return true;
}
}
boolean save_txt_file = db.add_txt_file(id,
readFile(path));
if (save_txt_file){
Toast.makeText(this, "saved successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "failed", Toast.LENGTH_SHORT).show();
}
private byte[] readFile(String file) {
ByteArrayOutputStream bos = null;
try {
File f = new File(Environment.getExternalStorageDirectory(),file);
FileInputStream fis = new FileInputStream(f);
byte[] buffer = new byte[1024];
bos = new ByteArrayOutputStream();
for (int len; (len = fis.read(buffer)) != -1;) {
bos.write(buffer, 0, len);
}
} catch (FileNotFoundException e) {
Log.e("TAG","error " + e.getMessage());
} catch (IOException e2) {
System.err.println(e2.getMessage());
}
return bos != null ? bos.toByteArray() : null;
}
path
在日志 Download/Link_on.txt
和 readFile(path)
returns [49, 10, 48, 48, 58, 48......但它仍然无法将文本文件保存在数据库中。感谢您的帮助。
在 table 的 CREATE
语句中,您已将列 POSITION
定义为 NOT NULL
,但在方法 add_txt_file()
中您没有提供一个值。
添加如下行:
cv.put(POSITION, "some value");