我已插入正确的查询并在SQlite浏览器中对其进行了测试,但数据未通过Java中的硬编码插入到表中。这是我的插入查询:Cursor c = db1.rawQuery(“insert into PackingList('ItemType')values('”+ getName +“')”,null);
public class PackingChecklist extends Activity {
ArrayList<category> products = new ArrayList<category>();
categoryadapter boxAdapter;
Button btn;
private String getName;
String DB_PATH;
final Context context=this;
private SQLiteDatabase mDataBase;
private static String DB_NAME ="test.db";
SQLiteDatabase db1;
private String selected;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.category);
boxAdapter = new categoryadapter(this, products);
/*ListView lvMain = (ListView) findViewById(R.id.lvMain);
lvMain.setAdapter(boxAdapter);*/
btn=(Button)findViewById(R.id.btnadd);
final Context context = this;
DBMain db;
db = new DBMain(this);
try {
db.createDB();
} catch (IOException ioe) {
throw new Error("Database not created....");
}
try {
db.openDB();
}catch(SQLException sqle){
throw sqle;
}
db1=openOrCreateDatabase("test",SQLiteDatabase.CREATE_IF_NECESSARY,null);
Cursor c= db1.rawQuery("select * from PackingList",null);
c.moveToFirst();
String temp="";
while(! c.isAfterLast())
{
String s3=c.getString(1);
temp="\n ItemName:"+s3;
c.moveToNext();
products.add(new category(temp));
}
boxAdapter = new categoryadapter(this, products);
ListView lvMain = (ListView) findViewById(R.id.lvMain);
lvMain.setAdapter(boxAdapter);
//click to add function
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//get the dialog view
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.insert);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// get user input and set it to result
// edit text
getName=userInput.getText().toString();
products.add(new category(getName));
boxAdapter.notifyDataSetChanged();
Cursor c= db1.rawQuery("insert into PackingList ('ItemType')values('"+getName+"')" ,null);
// db1.execSQL("insert into PackingList (ItemType)values('"+getName+"')" );
/*String d="insert into PackingList (ItemType)values('"+getName+"')";
db1.execSQL(d);*/
/*ContentValues cv = new ContentValues();
cv.put("ItemType", getName);
db1.insert("PackingList", null, cv); */
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
//delete function
lvMain.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
selected = ((TextView) arg1.findViewById(R.id.textView1)).getText().toString();
AlertDialog.Builder ad = new AlertDialog.Builder(PackingChecklist.this);
ad.setTitle("Delete?");
ad.setMessage("Are you sure you want to delete " + selected+"?");
final int positionToRemove = arg2;
ad.setNegativeButton("Cancel", null);
ad.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which)
{
products.remove(positionToRemove);
boxAdapter.notifyDataSetChanged();
String d="DELETE FROM PackingList WHERE ItemType='"+selected+"'";
db1.execSQL(d);
}
});
ad.show();
return false;
}
});
//click item
lvMain.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent i = new Intent(PackingChecklist.this, PackingContent.class);
startActivity(i);
}
});
}
}
这是我的数据库帮助程序类 公共类DBMain扩展了SQLiteOpenHelper {
private static String DB_PATH= "data/data/com.example.trippreparationmanager/databases/";
private static String DB_NAME = "test";
private SQLiteDatabase dbObj;
private final Context context;
public DBMain(Context context) {
super(context, DB_NAME , null, 3);
this. context = context;
}
public void createDB() throws IOException {
this.getReadableDatabase();
Log.i("Readable ends....................","end");
try {
copyDB();
Log.i("copy db ends....................","end");
} catch (IOException e) {
throw new Error("Error copying database");
}
}
private boolean checkDB(){
SQLiteDatabase checkDB = null;
try{
String path = DB_PATH + DB_NAME;
Log.i("myPath ......",path);
checkDB = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
Log.i("myPath ......",path);
if (checkDB!=null)
{
Cursor c= checkDB.rawQuery("select * from PackingList", null);
Log.i("Cursor.......",c.getString(0));
c.moveToFirst();
String contents[]=new String[80];
int flag=0;
while(! c.isAfterLast())
{
String temp="";
String s2=c.getString(0);
String s3=c.getString(1);
temp=temp+"\n ListId:"+s2+"\tItemType:"+s3;
contents[flag]=temp;
flag=flag+1;
Log.i("DB values.........",temp);
c.moveToNext();
}
}
else
{
return false;
}
}catch(SQLiteException e){
e.printStackTrace();
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
public void copyDB() throws IOException{
try {
Log.i("inside copyDB....................","start");
InputStream ip = context.getAssets().open(DB_NAME+".db");
Log.i("Input Stream....",ip+"");
String op= DB_PATH + DB_NAME ;
OutputStream output = new FileOutputStream( op);
byte[] buffer = new byte[1024];
int length;
while ((length = ip.read(buffer))>0){
output.write(buffer, 0, length);
Log.i("Content.... ",length+"");
}
output.flush();
output.close();
ip.close();
}
catch (IOException e) {
Log.v("error", e.toString());
}
}
public void openDB() throws SQLException {
String myPath = DB_PATH + DB_NAME;
dbObj = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
Log.i("open DB......",dbObj.toString());
}
@Override
public synchronized void close() {
if(dbObj != null)
dbObj.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}