class
DatabaseHelper
extends
SQLiteOpenHelper
{
private
static
DatabaseHelper
mInstance
=
null
/**
数据库名称
**/
public
static
final
String
DATABASE_NAME
=
"xys.db"
/**
数据库版本号
**/
private
static
final
int
DATABASE_VERSION
=
1
/**数据库SQL语句
添加一个表**/
private
static
final
String
NAME_TABLE_CREATE
=
"create
table
test("
+
"_id
INTEGER
PRIMARY
KEY
AUTOINCREMENT,"
+
"name
TEXT,"+"hp
INTEGER
DEFAULT
100,"+
"mp
INTEGER
DEFAULT
100,"
+
"number
INTEGER)"
DatabaseHelper(Context
context)
{
super(context,
DATABASE_NAME,
null,
DATABASE_VERSION)
}
/**单例模式**/
static
synchronized
DatabaseHelper
getInstance(Context
context)
{
if
(mInstance
==
null)
{
mInstance
=
new
DatabaseHelper(context)
}
return
mInstance
}
@Override
public
void
onCreate(SQLiteDatabase
db)
{
/**向数据中添加表**/
db.execSQL(NAME_TABLE_CREATE)
}
@Override
public
void
onUpgrade(SQLiteDatabase
db,
int
oldVersion,
int
newVersion)
{
/**可以拿到当前数据库的版本信息
与之前数据库的版本信息
用来更新数据库**/
}
/**
*
删除数据库
*
@param
context
*
@return
*/
public
boolean
deleteDatabase(Context
context)
{
return
context.deleteDatabase(DATABASE_NAME)
}
}
These files will be ones that get deleted first when the device runs low on storage. There is no guarantee when these files will be deleted.但是,最好不要依赖系统来管理,应该自己设定一个最大容量,当超出这个值时自己删除。
Context.getFilesDir(),Context.openFileOutput(String, int),Context.getFileStreamPath(String),Context.getDir(String, int)
/data/data/files
Android支持在SD卡上的应用私有目录,在Froyo版本后,通过getExternalFilesDir()可以获得具体路径。该路径依赖与应用的包名,如果你包为hello.file那么SD开上的应用私有目录为\mnt\sdcard\Android\data\hello.file\files\.
在使用SD卡目录时,需注意SD卡是否挂载,可通过Environment.getExternalStorageState()方法进行判断,如果返回值为Envirnment.MEDIA_MOUNTED表示SD卡处于挂载状态,可以放心使用。
getExternalCacheDir()和getCacheDir()比较
共同点:
files will be deleted when the application is uninstalled
不同点:
1、The platform does not monitor the space available in external storage, and thus will not automatically delete these files. Note that you should be managing the maximum space you will use for these anyway, just like with getCacheDir().
2、External files are not always available: they will disappear if the user mounts the external storage on a computer or removes it. See the APIs on Environment for information in the storage state.
3、There is no security enforced with these files. All applications can read and write files placed here.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)