发布网友 发布时间:2022-04-07 18:19
共6个回答
懂视网 时间:2022-04-07 22:41
package com.example.sqliteopenhelper; 2 3 import android.content.Context; 4 import android.database.sqlite.SQLiteDatabase; 5 import android.database.sqlite.SQLiteDatabase.CursorFactory; 6 import android.database.sqlite.SQLiteOpenHelper; 7 import android.util.Log; 8 9 public class MydbOpenHelper extends SQLiteOpenHelper{ 10 11 public MydbOpenHelper(Context context, String name, CursorFactory factory, int version){ 12 super(context, name, factory, version); 13 } 14 15 @Override 16 public void onCreate(SQLiteDatabase db) { 17 String createtable = "create table user(id int, name varchar(20), password char(8))"; 18 db.execSQL(createtable); 19 System.out.println("MydbOpenHelper.onCreate()"); 20 21 } 22 23 @Override 24 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 25 Log.i("MydbOpenHelper","oldVersion" + oldVersion + "newVersion" + newVersion); 26 System.out.println("MydbOpenHelper.onUpgrade()"); 27 28 } 29 } MydbOpenHelper
1 package com.example.sqliteopenhelper; 2 3 import android.app.Activity; 4 import android.content.ContentValues; 5 import android.database.Cursor; 6 import android.database.sqlite.SQLiteDatabase; 7 import android.os.Bundle; 8 import android.util.Log; 9 import android.view.Menu; 10 import android.view.MenuItem; 11 import android.view.View; 12 13 public class MainActivity extends Activity { 14 SQLiteDatabase db = null; 15 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 21 MydbOpenHelper dbhelper = new MydbOpenHelper(this, "userinfo.db", null, 1); 22 db = dbhelper.getWritableDatabase(); 23 24 } 25 26 public void insert(View v){ 27 ContentValues c = new ContentValues(); 28 //c.put("id", 2); 29 //c.put("name", "user222"); 30 //c.put("password", "222"); 31 32 c.put("id", 1); 33 c.put("name", "user1"); 34 c.put("password", "222"); 35 36 db.insert("user", null, c); 37 } 38 public void delete(View v){ 39 db.delete("user", "id=? and name=?", new String[]{"1","user1"}); 40 } 41 public void update(View v){ 42 ContentValues c = new ContentValues(); 43 c.put("name", "user222"); 44 c.put("password", "88888888"); 45 db.update("user", c, "id=?", new String[]{"2"}); 46 } 47 public void query(View v){ 48 String[] colums = {"id", "name", "password"}; 49 Cursor c = db.query("user", colums, null, null, null, null, null); 50 51 while(c.moveToNext()){ 52 int id = c.getInt(c.getColumnIndex("id")); 53 String username = c.getString(c.getColumnIndex("name")); 54 String password = c.getString(c.getColumnIndex("password")); 55 Log.i("sqliteopenhelper", id + "," +username+"," +password); 56 } 57 } 58 }MainActivity.java
006_02SQLite_OpenHelper
标签:
热心网友 时间:2022-04-07 19:49
要在Android系统中操作SQLite数据库,是通过Android的核心类SQLiteDatabase类来实现的,通常情况下为了数据库升级的需要以及使用方便,我们会选择继承SQLiteOpenHelper抽像类,但是SQLiteOpenHelper会将数据库文件创建在一个固定的目录(内存的/data/data/<package name/databases>目录中),如果你想使用已经存在的数据库文件也就是说数据库会和程序一起发布,就得通过使用SQLiteDabase的静态方法OpenOrCreateDatabase()方法来得到SQLiteDabase对象,下面是一个具体操作类:
热心网友 时间:2022-04-07 21:07
Before trying this code, please find this line in below code:
private static String DB_NAME ="YourDbName";// Database name
DB_NAME here is the name of your database. It is assumed that you have a copy of the database in the assets folder, so for example if your database name is ordersDB, then the value of DB_NAME will be ordersDB,
private static String DB_NAME ="ordersDB";
Keep the Database in assets folder & then follow below:
DataHelper class:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DataBaseHelper extends SQLiteOpenHelper
{
private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window
//destination path (location) of our database on device
private static String DB_PATH = "";
private static String DB_NAME ="YourDbName";// Database name
private SQLiteDatabase mDataBase;
private final Context mContext;
public DataBaseHelper(Context context)
{
super(context, DB_NAME, null, 1);// 1? its Database Version
if(android.os.Build.VERSION.SDK_INT >= 4.2){
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
}
else
{
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
}
this.mContext = context;
}
public void createDataBase() throws IOException
{
//If database not exists copy it from the assets
boolean mDataBaseExist = checkDataBase();
if(!mDataBaseExist)
{
this.getReadableDatabase();
this.close();
try
{
//Copy the database from assests
copyDataBase();
Log.e(TAG, "createDatabase database created");
}
catch (IOException mIOException)
{
throw new Error("ErrorCopyingDataBase");
}
}
}
//Check that the database exists here: /data/data/your package/databases/Da Name
private boolean checkDataBase()
{
File dbFile = new File(DB_PATH + DB_NAME);
//Log.v("dbFile", dbFile + " "+ dbFile.exists());
return dbFile.exists();
}
//Copy the database from assets
private void copyDataBase() throws IOException
{
InputStream mInput = mContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream mOutput = new FileOutputStream(outFileName);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer))>0)
{
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
}
//Open the database, so we can query it
public boolean openDataBase() throws SQLException
{
String mPath = DB_PATH + DB_NAME;
//Log.v("mPath", mPath);
mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
//mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
return mDataBase != null;
}
@Override
public synchronized void close()
{
if(mDataBase != null)
mDataBase.close();
super.close();
}
}
Write a DataAdapter class like:
import java.io.IOException;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class TestAdapter
{
protected static final String TAG = "DataAdapter";
private final Context mContext;
private SQLiteDatabase mDb;
private DataBaseHelper mDbHelper;
public TestAdapter(Context context)
{
this.mContext = context;
mDbHelper = new DataBaseHelper(mContext);
}
public TestAdapter createDatabase() throws SQLException
{
try
{
mDbHelper.createDataBase();
}
catch (IOException mIOException)
{
Log.e(TAG, mIOException.toString() + " UnableToCreateDatabase");
throw new Error("UnableToCreateDatabase");
}
return this;
}
public TestAdapter open() throws SQLException
{
try
{
mDbHelper.openDataBase();
mDbHelper.close();
mDb = mDbHelper.getReadableDatabase();
}
catch (SQLException mSQLException)
{
Log.e(TAG, "open >>"+ mSQLException.toString());
throw mSQLException;
}
return this;
}
public void close()
{
mDbHelper.close();
}
public Cursor getTestData()
{
try
{
String sql ="SELECT * FROM myTable";
Cursor mCur = mDb.rawQuery(sql, null);
if (mCur!=null)
{
mCur.moveToNext();
}
return mCur;
}
catch (SQLException mSQLException)
{
Log.e(TAG, "getTestData >>"+ mSQLException.toString());
throw mSQLException;
}
}
}
Now you can use it like:
TestAdapter mDbHelper = new TestAdapter(urContext);
mDbHelper.createDatabase();
mDbHelper.open();
Cursor testdata = mDbHelper.getTestData();
mDbHelper.close();
NOTE:
For Jellybean 4.2 change:
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
to:
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
in the DataHelper class this code will work on JB 4.2 multi users.
热心网友 时间:2022-04-07 22:41
使用SQLiteOpenHelper的好处就是随着你应用的卸载数据库也一同卸载的,如果是拷入Sdcard的话还得代码操作。。。热心网友 时间:2022-04-08 00:33
如果你放在res中是找不到的 去搜个关于操作SQLiteOpenHelper的小DEMO.热心网友 时间:2022-04-08 02:41
如果你是通过SQLiterOpenHelper创建的应该在Data\data\你的包名里面的database里面