十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
1.

創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)與策劃設(shè)計(jì),宣威網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:宣威等地區(qū)。宣威做網(wǎng)站價(jià)格咨詢:18980820575
創(chuàng)建數(shù)據(jù)庫(kù):sqlite3.exe
mySqlite.db
2.
創(chuàng)建表:
sqlitecreate
table
person
(
_id
integer
primary
key
,
name
varchar(50),
age
integer,
sex
char(2)
);
3.
插入數(shù)據(jù):
sqliteinsert
into
person
values(1,'劉備',38,'男');
sqliteinsert
into
person
values(2,'關(guān)羽',42,'男');
sqliteinsert
into
person
values(3,'大喬',20,'女');
4.
備份:
sqlite.backup
D:/mydb.db
5.
恢復(fù)數(shù)據(jù):
sqlite.restore
mydb.db
6.
查看表:
sqlite.tables
建立數(shù)據(jù)庫(kù)
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);
}
}
Android中提供4種數(shù)據(jù)存儲(chǔ)的方法:(1)SharedPreferences,用鍵值對(duì)的方式來(lái)存儲(chǔ)數(shù)據(jù),是一種輕量級(jí)的存儲(chǔ)機(jī)制,可以存儲(chǔ)一些屬性等。(2)Files:文件輸入輸出流的方式存儲(chǔ)數(shù)據(jù),F(xiàn)ileInputStream和FileOutputStream。在Android中,文件是一個(gè)應(yīng)用程序私有的,一個(gè)應(yīng)用程序無(wú)法讀寫其他應(yīng)用程序的文件。寫入SD卡除外。(3)SQLite(4)網(wǎng)絡(luò)(這個(gè)不算吧~~~--!)。
SQLite:
(1)創(chuàng)建和打開數(shù)據(jù)庫(kù)可以使用方法openOrCreateDatabase,它會(huì)自動(dòng)去檢測(cè)是否存在數(shù)據(jù)庫(kù),如果存在則打開,如果不存在則創(chuàng)建一個(gè)數(shù)據(jù)庫(kù);成功返回一個(gè)SQliteDatabase對(duì)象。(2)創(chuàng)建一張表通過(guò)SQL語(yǔ)句實(shí)現(xiàn),調(diào)用sqliteDatabase對(duì)象的execSQL方法,執(zhí)行創(chuàng)建表的SQL語(yǔ)句。
(3)向數(shù)據(jù)表中添加一條記錄可以直接通過(guò)SQL語(yǔ)句實(shí)現(xiàn),也可以使用ContentValue對(duì)象,ContentValue對(duì)象是一個(gè)Map,Key是字段名,Value是值。Cv.put(key,value);然后調(diào)用sqliteDatabase對(duì)象的
insert(tableName,null,cv)方法插入數(shù)據(jù)。
(4)刪除數(shù)據(jù)可以直接執(zhí)行SQL,也可以執(zhí)行sqliteDatabase的delete方法。
(5)同理修改數(shù)據(jù)也是執(zhí)行SQL或調(diào)用update方法,需要傳入ContenValue的對(duì)象表示修改的內(nèi)容。
(6)關(guān)閉數(shù)據(jù)庫(kù)sqliteDatabase.close();
(7)刪除指定表調(diào)用SQL語(yǔ)句即可。
(8)查詢:在Android中查詢數(shù)據(jù)是通過(guò)Cursor類來(lái)實(shí)現(xiàn)的,當(dāng)我們使用SQLiteDatabase.query()方法時(shí),會(huì)得到一個(gè)Cursor對(duì)象,Cursor對(duì)象指向的是每條數(shù)據(jù)。例如cur.moveToFirst();cur.moveToNext();等。在實(shí)際開發(fā)中,為了能夠更好地管理和維護(hù)數(shù)據(jù)庫(kù),我們會(huì)封裝一個(gè)繼承自SQLiteOpenHelper類的數(shù)據(jù)庫(kù)操作類。SQLiteOpenHelper的構(gòu)造方法中分別需要傳入Context、數(shù)據(jù)庫(kù)名稱、CursorFactory(一般默認(rèn)null)、數(shù)據(jù)庫(kù)版本號(hào)。在SQLiteOpenHelper中首先執(zhí)行的是onCreate方法(當(dāng)數(shù)據(jù)庫(kù)第一次被創(chuàng)建時(shí))。在構(gòu)造函數(shù)中并沒有真正創(chuàng)建數(shù)據(jù)庫(kù),而是調(diào)用getWriteableDatabase或者getReadableDatabase方法時(shí)才真正去創(chuàng)建數(shù)據(jù)庫(kù),并且返回一個(gè)SQLiteDatabase對(duì)象。
Android提供了5種方式來(lái)讓用戶保存持久化應(yīng)用程序數(shù)據(jù)。根據(jù)自己的需求來(lái)做選擇,比如數(shù)據(jù)是否是應(yīng)用程序私有的,是否能被其他程序訪問(wèn),需要多少數(shù)據(jù)存儲(chǔ)空間等,分別是:
① 使用SharedPreferences存儲(chǔ)數(shù)據(jù)
② 文件存儲(chǔ)數(shù)據(jù)
③ SQLite數(shù)據(jù)庫(kù)存儲(chǔ)數(shù)據(jù)
④ 使用ContentProvider存儲(chǔ)數(shù)據(jù)
⑤ 網(wǎng)絡(luò)存儲(chǔ)數(shù)據(jù)
一、新建外部SQLite數(shù)據(jù)庫(kù)
(1)下載并安裝 SQLite可視化管理工具(SQLite Expert Pro) v3.4.17 破解版
(2)將你手頭上的數(shù)據(jù)放到EXCEL表格中,保存為CSV格式的數(shù)據(jù)
(3)在此工具中按照你現(xiàn)有的數(shù)據(jù)格式新建數(shù)據(jù)庫(kù)和表,如數(shù)據(jù)庫(kù)為:contact.db,表為employee
(4)通過(guò)此工具菜單欄中Import/Export下的Import text file(CSV,TSC)功能,將你現(xiàn)有的CSV數(shù)據(jù)導(dǎo)入到你新建的數(shù)據(jù)表中(主要目的是省的一個(gè)一個(gè)的錄入了)
二、在eclipse中新建一個(gè)android app工程,并在新建的工程文件夾點(diǎn)右鍵new-folder,在res文件夾下新建raw文件夾(如果有就不用新建了)
三、用鼠標(biāo)將新建的SQLite數(shù)據(jù)庫(kù)文件contact.db拖動(dòng)到新建工程的res下的raw文件下,出現(xiàn)提示,選擇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對(duì)象
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=0;i
{
String ziduan1=c.getString(0);//字段1的數(shù)據(jù)
String ziduan2=c.getString(1);//字段1的數(shù)據(jù)
}
}
------------------------------------------------
//前面用到的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("創(chuàng)建失敗");
}
}
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();
}
}
}
五、程序發(fā)布
按照以上方式,可以將外部建的SQLite數(shù)據(jù)庫(kù)成功的發(fā)布出來(lái)