如何在资产数据库中插入数据 - Android Eclipse

时间:2015-01-15 19:26:03

标签: java android eclipse sqlite

我是sqlite数据库浏览器的新手,我已经完成了检索来自数据库的信息,但我不知道如何使用edittext插入数据。还有一件事,我不知道为什么我的数据库在运行时工作,但是当我查看数据/数据/包名/数据库时,什么都没有。请耐心等待。

DBHelper.java

 public class DBHelper extends SQLiteOpenHelper {  

 private static String DB_NAME = "trialeleventh"; 

 private static int DB_Version = 2;
 private SQLiteDatabase db;  
 private final Context context;  
 private String DB_PATH = "/data/data/packagename/databases/";  

 public DBHelper(Context context) {  
  super(context, DB_NAME, null, DB_Version);  
  this.context = context;  
 // DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";  
 }  

 public void createDataBase() throws IOException {  

          boolean dbExist = checkDataBase();  
          if (dbExist) {  
         try {  
           copyDataBase();  
          } catch (IOException e) {  
           throw new Error("Error copying database");  
          }  
          } else {  
         SQLiteDatabase db = this.getWritableDatabase();
         if (db.isOpen()){
             db.close();
             try {  
               copyDataBase();  
              } catch (IOException e) {  
               throw new Error("Error copying database");  
              }  
         } else {
         try {  
           copyDataBase();  
          } catch (IOException e) {  
           throw new Error("Error copying database");  
          }  
         }
          }
          }  

 private boolean checkDataBase() {  
  File dbFile = new File(DB_PATH + DB_NAME);  
  return dbFile.exists();  
 }  

 private void copyDataBase() throws IOException {  

  InputStream myInput = context.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);  
  }  

  // Close the streams  
  myOutput.flush();  
  myOutput.close();  
  myInput.close();  

 }  
 public void openDataBase() throws SQLException{
        //Open the database
        String myPath = DB_PATH + DB_NAME;
        db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    }


 public Cursor getData() {  
  String myPath = DB_PATH + DB_NAME;  
  db = SQLiteDatabase.openDatabase(myPath, null,  
    SQLiteDatabase.OPEN_READWRITE);  
  onUpgrade(db, DB_Version, 2);
  Cursor c = db.rawQuery("SELECT * FROM studcomm", null);  
    return c;  
 }  

 @Override  
 public void onCreate(SQLiteDatabase arg0) {  
  // TODO Auto-generated method stub  
 }  

 @Override  
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
  // TODO Auto-generated method stub  
    Log.d ("onUpgrade first log", Integer.toString(db.getVersion()));

    if (oldVersion == 1) {

        DB_Version = 2;
        db.setVersion(2);
        Log.d ("onUpgrade second log", Integer.toString(db.getVersion()));

    }

    else {
        Log.d("onUpgrade", "else-clause: Already upgraded!");
 }  
 }

}

这是我的检索代码。

 public class DataListView extends Activity{  
 DBHelper dbhelper;  
 protected ListAdapter adapter;
 @Override  
public void onCreate(Bundle savedInstanceState) {  
super.onCreate(savedInstanceState);  
setContentView(R.layout.mainact);    
String[] from = new String[] { "_id", "comm" };  
int[] to = new int[] { R.id.TextView1, R.id.TextView2};  

 dbhelper = new DBHelper(this);  
 try {  
  dbhelper.createDataBase();  
 } catch (IOException e) {  
// TODO Auto-generated catch block  
 e.printStackTrace();  
}  

Cursor c = dbhelper.getData();  

 adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.list, c, from, to);  

ListView list = (ListView) findViewById(R.id.ListView1);  

list.setAdapter(adapter);  


 Button button = (Button) findViewById(R.id.insert);
button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

            Intent i = new Intent(DataListView.this, Insert.class);
            startActivity(i);


    }



     });
 }

 }

我没有Insert.java的任何代码。我甚至不知道我会编码什么。请帮我。感谢。

0 个答案:

没有答案