我在资产中有一个sqlite db文件,然后导入到应用程序中。select查询可以很好地工作并显示结果,但同时,如果我尝试将数据插入表中,它将不起作用。我没有出错,这样我就可以找出问题所在。
databasehelper类:addproject()方法用于插入数据。
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DB_VERSION=1;
private static String DB_PATH="";
private static final String DB_NAME="AndroidProject.sqlite";
//Projects Table attrib
String TABLE_PROJECT="Project";
String KEY_PROJECT_ID = "_id";
String KEY_PROJECT_NAME = "project_name";
String KEY_PROJECT_DESC = "project_desc";
String KEY_PROJECT_TYPE = "project_type";
String KEY_PROJECT_START_DATE = "start_date";
String KEY_PROJECT_END_DATE = "end_date";
private SQLiteDatabase myDatabase;
private final Context myContext;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
if (Build.VERSION.SDK_INT>=15){
DB_PATH=context.getApplicationInfo().dataDir + "/databases/";
}
else {
DB_PATH= Environment.getDataDirectory() + "/data/" + context.getPackageName() + "/databases/";
}
this.myContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void checkAndCopyDatabase(){
boolean doExists=checkDatabase();
if (doExists){
Log.d("TAG", "Database alredy exists");
}
else{
this.getReadableDatabase();
}
try {
copyDatabase();
} catch (IOException e) {
e.printStackTrace();
Log.d("TAG", "Error Copying DATABASE");
}
}
public boolean checkDatabase(){
SQLiteDatabase checkDB=null;
try {
String myPath=DB_PATH+DB_NAME;
checkDB=SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE);
}catch (SQLiteException e){
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
public void copyDatabase() throws IOException {
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length=myInput.read(buffer))>0){
myOutput.write(buffer,0,length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDatabase(){
String myPath = DB_PATH + DB_NAME;
myDatabase=SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE);
}
public synchronized void close(){
if (myDatabase != null){
myDatabase.close();
}
super.close();
}
public Cursor QueryData(String query){
return myDatabase.rawQuery(query,null);
}
public void addProject(SqlProjects blog){
//SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_PROJECT_NAME, blog.get_project_name());
values.put(KEY_PROJECT_DESC, blog.get_project_desc());
values.put(KEY_PROJECT_TYPE, blog.get_project_type());
values.put(KEY_PROJECT_START_DATE, blog.get_project_start_date());
values.put(KEY_PROJECT_END_DATE, blog.get_project_end_date());
try {
Log.d("TAG", "Adding data");
myDatabase.insertOrThrow(TABLE_PROJECT, null, values);
} catch (SQLiteException e) {
e.printStackTrace();
}
myDatabase.close();
}
}
sqlprojects类构造函数、getter和setter
public class SqlProjects {
private DatabaseHelper helper;
int _id;
String _project_name, _project_desc,_project_type;
String _project_start_date,_project_end_date;
public SqlProjects() {
}
public SqlProjects(String _project_name, String _project_desc, String _project_type, String _project_start_date, String _project_end_date) {
this._project_name = _project_name;
this._project_desc = _project_desc;
this._project_type = _project_type;
this._project_start_date = _project_start_date;
this._project_end_date = _project_end_date;
}
public SqlProjects(int _id, String _project_name, String _project_desc, String _project_type, String _project_start_date, String _project_end_date) {
this._id = _id;
this._project_name = _project_name;
this._project_desc = _project_desc;
this._project_type = _project_type;
this._project_start_date = _project_start_date;
this._project_end_date = _project_end_date;
}
public int get_project_id() {
return _id;
}
public void set_project_id(int _project_id) {
this._id = _project_id;
}
public String get_project_name() {
return _project_name;
}
public void set_project_name(String _project_name) {
this._project_name = _project_name;
}
public String get_project_desc() {
return _project_desc;
}
public void set_project_desc(String _project_desc) {
this._project_desc = _project_desc;
}
public String get_project_type() {
return _project_type;
}
public void set_project_type(String _project_type) {
this._project_type = _project_type;
}
public String get_project_start_date() {
return _project_start_date;
}
public void set_project_start_date(String _project_start_date) {
this._project_start_date = _project_start_date;
}
public String get_project_end_date() {
return _project_end_date;
}
public void set_project_end_date(String _project_end_date) {
this._project_end_date = _project_end_date;
}
}
主要类别
final DatabaseHelper db = new DatabaseHelper(Add_Project.this);
btnAddProjects.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!(TextUtils.isEmpty(editName.getText().toString())) && !(TextUtils.isEmpty(editDesc.getText().toString())) && !(tvStartDate.getText().equals("")) && !(tvEndDate.getText().equals(""))) {
db.addProject(new SqlProjects(editName.getText().toString(), editDesc.getText().toString(), editType.getSelectedItem().toString(), tvStartDate.getText().toString(), tvEndDate.getText().toString()));
editName.setText("");
editDesc.setText("");
tvStartDate.setText("");
tvEndDate.setText("");
Toast.makeText(getApplicationContext(), "Added Successfully", Toast.LENGTH_SHORT).show();
onBackPressed();
}
}
});
1条答案
按热度按时间mftmpeh81#
我想你在执行插入操作之前忘了打开数据库。
看来问题出在哪了看看吧!