【发布时间】:2018-11-12 07:38:49
【问题描述】:
我创建了一个 sqlite 数据库并将其放在我的 android 项目的 assets 文件夹中。现在我想在这个数据库上执行Insert,Delete,Retrieve 操作。
我创建了一个新活动,我将在其中输入一个人的详细信息,然后我将按下保存按钮 (checkout_btn) 以将该信息保存在现有数据库中。
我不知道这段代码有什么问题。我没有收到任何错误,甚至数据也没有插入到现有数据库中。
我还想根据此应用程序中用户功能提供的搜索框值添加从数据库中检索数据(on button click)。
这是我的代码:
This class talk about my database which is already present in my assets folder in my project. In this database only i have to insert my data from a form.
**DBConstant.Java**
public abstract class DBConstant
{ //database file directory
public static String DATABASE_PATH = "/data/data/activity.test/databases";
//database file name
public static String DATABASE_FILE = "test.db";
//database version
public static int DATABASE_VERSION = 1;
}
**This is my DBOpenHelper.Java file**
**DBOpenHelper.Java**
public class DBOpenHelper extends SQLiteOpenHelper {
public DBOpenHelper(Context context, String path, int version){
super(context, path, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
**Below is my DBOperator.Java file**
/**
* Class to manipulate tables & data
* Uses singleton pattern to create single instance
*/
public class DBOperator
{
private static DBOperator instance = null;
private SQLiteDatabase db;
private DBOperator()
{
//path of database file
String path = DBConstant.DATABASE_PATH + "/" + DBConstant.DATABASE_FILE;
db = SQLiteDatabase.openDatabase(path, null,
SQLiteDatabase.OPEN_READWRITE);
}
/*
* Singleton Pattern
* Why should we avoid multiple instances here?
*/
public static DBOperator getInstance()
{
if (instance==null) instance = new DBOperator();
return instance;
}
/**
* Copy database file
* From assets folder (in the project) to android folder (on device)
*/
public static void copyDB(Context context) throws
IOException,FileNotFoundException{
String path = DBConstant.DATABASE_PATH + "/" + DBConstant.DATABASE_FILE;
File file = new File(path);
if (!file.exists()){
DBOpenHelper dbhelper = new DBOpenHelper(context, path ,1);
dbhelper.getWritableDatabase();
InputStream is = context.getAssets().open(DBConstant.DATABASE_FILE);
OutputStream os = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer))>0){
os.write(buffer, 0, length);
}
is.close();
os.flush();
os.close();
}
}
/**
* execute sql without returning data, such as alter
* @param sql
*/
public void execSQL(String sql) throws SQLException
{
db.execSQL(sql);
}
/**
* execute sql such as update/delete/insert
* @param sql
* @param args
* @throws SQLException
*/
public void execSQL(String sql, Object[] args) throws SQLException
{
db.execSQL(sql, args);
}
/**
* execute sql query
* @param sql
* @param selectionArgs
* @return cursor
* @throws SQLException
*/
public Cursor execQuery(String sql,String[] selectionArgs) throws
SQLException
{
return db.rawQuery(sql, selectionArgs);
}
/**
* execute query without arguments
* @param sql
* @return
* @throws SQLException
*/
public Cursor execQuery(String sql) throws SQLException
{
return this.execQuery(sql, null);
}
/**
* close database
*/
public void closeDB()
{
if (db!=null) db.close();
}
}
Here is my DBOperator.Java
/**
* Class to manipulate tables & data
* Uses singleton pattern to create single instance
*/
public class DBOperator
{
private static DBOperator instance = null;
private SQLiteDatabase db;
private DBOperator()
{
//path of database file
String path = DBConstant.DATABASE_PATH + "/" + DBConstant.DATABASE_FILE;
db = SQLiteDatabase.openDatabase(path, null,
SQLiteDatabase.OPEN_READWRITE);
}
/*
* Singleton Pattern
* Why should we avoid multiple instances here?
*/
public static DBOperator getInstance()
{
if (instance==null) instance = new DBOperator();
return instance;
}
/**
* Copy database file
* From assets folder (in the project) to android folder (on device)
*/
public static void copyDB(Context context) throws
IOException,FileNotFoundException{
String path = DBConstant.DATABASE_PATH + "/" + DBConstant.DATABASE_FILE;
File file = new File(path);
if (!file.exists()){
DBOpenHelper dbhelper = new DBOpenHelper(context, path ,1);
dbhelper.getWritableDatabase();
InputStream is = context.getAssets().open(DBConstant.DATABASE_FILE);
OutputStream os = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer))>0){
os.write(buffer, 0, length);
}
is.close();
os.flush();
os.close();
}
}
/**
* execute sql without returning data, such as alter
* @param sql
*/
public void execSQL(String sql) throws SQLException
{
db.execSQL(sql);
}
/**
* execute sql such as update/delete/insert
* @param sql
* @param args
* @throws SQLException
*/
public void execSQL(String sql, Object[] args) throws SQLException
{
db.execSQL(sql, args);
}
/**
* execute sql query
* @param sql
* @param selectionArgs
* @return cursor
* @throws SQLException
*/
public Cursor execQuery(String sql,String[] selectionArgs) throws
SQLException
{
return db.rawQuery(sql, selectionArgs);
}
/**
* execute query without arguments
* @param sql
* @return
* @throws SQLException
*/
public Cursor execQuery(String sql) throws SQLException
{
return this.execQuery(sql, null);
}
/**
* close database
*/
public void closeDB()
{
if (db!=null) db.close();
}
}
NewActivity.java
public class NewpActivity extends AppCompatActivity
{
String PaFirstName,PaLastName,PaDOB,PaGender,PaContact,PaStreetAPT,PaCity,PaState,country,PaPincode,PaInsurance;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_newp);
Button signUpBtn = (Button) findViewById(R.id.checkout_btn);
signUpBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//Running method for updating string variables from input boxes
getValues();
DBOperator.getInstance().execSQL(SQLCommand.NEW_USER, getArgs());
Toast.makeText(getBaseContext(), "Checkout successfully", Toast.LENGTH_SHORT).show();
}
});
}
SQLCommand.java
public abstract class SQLCommand {
public static String NEW_USER = "insert into Patient(PaFirstName,PaLastName,PaDOB,PaGender,PaContact,PaStreetAPT,PaCity,PaState,PaPincode,PaInsurance) values(?,?,?,?,?,?,?,?,?,?)";
}
【问题讨论】:
-
您的路径变量指向本地 db 文件夹而不是 assests 文件夹,您确定要使用该数据库吗?
-
是的。它是我的应用程序中存在的本地数据库。本地数据库存储在 assets 文件夹中(我在我的应用程序中创建以在本地存储我的数据库)
-
资产文件夹是只读的
标签: java android sqlite android-studio