① 使用SharedPreferences存储数据
② 文件存储数据
③ SQLite数据库存储数据
④ 使用ContentProvider存储数据
⑤ 网络存储数据
建立数据库public class WeatherDataBaseHelper extends SQLiteOpenHelper {
private static final String TAG = "pin"
// private static final String CREAT_TABLE_SQL =
// "create table IPTV(_id integer primary key autoincrement,"
// +"_name String not null, _path String not null)"
// private static final String CREAT_TABLE_SQL = "create table " +
// DataTools.TABLE_NAME + "("
// + DataTools._ID + " integer autoincrement,"
// + DataTools.NAME + " String not null,"
// + DataTools.PATH + " String not null,"
// + DataTools.HASHCODE + " integer "
// + "primary key(" + DataTools._ID + "," + DataTools.HASHCODE + ")"
// + ")"
private static final String CREAT_TABLE_SQL = "create table " + DataTools.TABLE_NAME + "("
+ DataTools._ID + " integer primary key,"
+ DataTools.CITY + " String not null,"
+ DataTools.TEMP_CUR + " String not null,"
+ DataTools.TEMP_L + " String not null,"
+ DataTools.TEMP_H + " String not null,"
+ DataTools.STATUS + " String not null,"
+ DataTools.IMAGE + " blob not null" + ")"// blob
/**
* @param context
*/
public WeatherDataBaseHelper(Context context) {
super(context, DataTools.DB_NAME, null, DataTools.DATABASE_VERSION)
// TODO Auto-generated constructor stub
}
/**
* @param context
* @param name
* @param factory
* @param version
*/
public WeatherDataBaseHelper(Context context, String name, int version) {
super(context, name, null, version)
// TODO Auto-generated constructor stub
Log.v(TAG, " WeatherDataBaseHelper-------constructor------------")
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.v(TAG, "WeatherDataBaseHelper on creat-------------------")
db.execSQL(CREAT_TABLE_SQL)
}
/**
* when version changed,updata database
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.v(TAG, "on upgrade")
String sql = " DROP TABLE IF EXISTS " + DataTools.TABLE_NAME
db.execSQL(sql)
onCreate(db)
}
}
一、新建外部SQLite数据库(1)下载并安装 SQLite可视化管理工具(SQLite Expert Pro) v3.4.17 破解版
http://www.cr173.com/soft/36343.html
(2)将你手头上的数据放到EXCEL表格中,保存为CSV格式的数据
(3)在此工具中按照你现有的数据格式新建数据库和表,如数据库为:contact.db,表为employee
(4)通过此工具菜单栏中Import/Export下的Import text file(CSV,TSC)功能,将你现有的CSV数据导入到你新建的数据表中(主要目的是省的一个一个的录入了)
二、在eclipse中新建一个android app工程,并在新建的工程文件夹点右键new->folder,在res文件夹下新建raw文件夹(如果有就不用新建了)
三、用鼠标将新建的SQLite数据库文件contact.db拖动到新建工程的res下的raw文件下,出现提示,选择copy
四、程序代码
private static final String DATABASE_PATH = "/data/data/你的主程序包路径(如:com.szair.contact)/databases"
private static final int DATABASE_VERSION = 0
private static final String DATABASE_NAME = "contact.db"
private static String outFileName = DATABASE_PATH + "/" + DATABASE_NAME
try {
buildDatabase()//见下
} catch (Exception e) {
e.printStackTrace()
}
//SQLiteDatabase对象
SQLiteDatabase db=SQLiteDatabase.openDatabase(outFileName, null,SQLiteDatabase.NO_LOCALIZED_COLLATORS)
String t="SELECT 字段名1,字段名2 FROM employee WHERE **** ORDER BY ***"
Cursor c =db.rawQuery(t, null)
if(c.moveToFirst()){
for(int i=0i
{
String ziduan1=c.getString(0)//字段1的数据
String ziduan2=c.getString(1)//字段1的数据
}
}
------------------------------------------------
//前面用到的buildDatabase方法
private void buildDatabase() throws Exception{
InputStream myInput = getResources().openRawResource(R.raw.sz_contact)
File file = new File(outFileName)
File dir = new File(DATABASE_PATH)
if (!dir.exists()) {
if (!dir.mkdir()) {
throw new Exception("创建失败")
}
}
if (!file.exists()) {
try {
OutputStream myOutput = new FileOutputStream(outFileName)
byte[] buffer = new byte[1024]
int length
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length)
}
myOutput.close()
myInput.close()
} catch (Exception e) {
e.printStackTrace()
}
}
}
五、程序发布
按照以上方式,可以将外部建的SQLite数据库成功的发布出来
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)