【发布时间】:2021-02-07 06:34:32
【问题描述】:
了解如何在 Android 应用中使用数据库。
我想了解如何将 SQL 数据库连接到 Android 应用程序。 就像 YouTube 和其他一些使用数据库的应用一样。
请帮我谈谈在 Android 应用中使用数据库。
谢谢大家
【问题讨论】:
-
“请点赞”是什么意思?
-
意义投票问题
了解如何在 Android 应用中使用数据库。
我想了解如何将 SQL 数据库连接到 Android 应用程序。 就像 YouTube 和其他一些使用数据库的应用一样。
请帮我谈谈在 Android 应用中使用数据库。
谢谢大家
【问题讨论】:
更多信息SQLiteDatabase,
使用此代码连接数据库
public class DBHelper extends SQLiteOpenHelper
{
private static String DB_NAME = "DataBase.sqlite";
private static String DB_PATH = "";
private static final int DB_VERSION = 63;
private SQLiteDatabase mDataBase;
private final Context mContext;
private boolean mNeedUpdate = false;
public DBHelper(Context context)
{
super(context, DB_NAME, null, DB_VERSION);
this.mContext = context;
DB_PATH = mContext.getApplicationInfo().dataDir + "/databases/";
boolean dbexist = checkDataBase();
if (dbexist)
{
openDataBase();
}
else
{
createDataBase();
}
}
private void createDataBase()
{
boolean dbexist = checkDataBase();
if (!dbexist)
{
this.getReadableDatabase();
this.close();
try
{
copyDataBase();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
private boolean checkDataBase()
{
boolean checkdb = false;
try
{
String myPath = DB_PATH + DB_NAME;
File dbfile = new File(myPath);
checkdb = dbfile.exists();
}
catch (SQLiteException e)
{
e.printStackTrace();
}
return checkdb;
}
private void copyDataBase() throws IOException
{
InputStream myinput = mContext.getAssets().open("G2DcfxJZdu/" + DB_NAME);
OutputStream myoutput = new FileOutputStream(DB_PATH + DB_NAME);
byte[] buffer = new byte[1024];
int length;
while ((length = myinput.read(buffer)) > 0)
{
myoutput.write(buffer, 0, length);
}
myoutput.flush();
myoutput.close();
myinput.close();
}
private void openDataBase() throws SQLException
{
String mypath = DB_PATH + DB_NAME;
mDataBase = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close()
{
if (mDataBase != null)
{
mDataBase.close();
}
super.close();
}
@Override
public void onCreate(SQLiteDatabase db)
{
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
if (newVersion > oldVersion)
{
mNeedUpdate = true;
try
{
updateDataBase();
}
catch (Exception exception)
{
exception.printStackTrace();
}
}
}
private void updateDataBase() throws IOException
{
if (mNeedUpdate)
{
File dbFile = new File(DB_PATH + DB_NAME);
if (dbFile.exists())
{
dbFile.delete();
}
copyDataBase();
mNeedUpdate = false;
}
}
@Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
db.disableWriteAheadLogging();
}
【讨论】: