Android学习笔记(35)--- 使用sqlite 把.txt文件导进.db

一、新建txt文件,以一下固定格式存储数据,如下:

1|1000|file:///system/kindergarten/item_main.html
2|1001|file:///system/kindergarten/item_main.html
3|1002|file:///system/kindergarten/item_main.html
4|1003|file:///system/kindergarten/item_main.html
5|1004|file:///system/kindergarten/item_main.html

二、android工程创建生成.db文件

1、DatabaseHelper.java

package com.ideal.txttodb;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {
	private static final int DATABASE_VERSION = 1;
	private String DATABASE_CREATE = "create table pathcode(_id INTEGER PRIMARY KEY AUTOINCREMENT,code varchar(100),path varchar(200))"; 

	public DatabaseHelper(Context context, String DATABASE_NAME) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		Log.i("txttodb", "DatabaseHelper--oncreate");
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		Log.i("txttodb", "sql--oncreate");
		db.execSQL(DATABASE_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}
}

2、DBManager.java
package com.ideal.txttodb;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DBManager {

	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;

	public DBManager(Context context) {
		Log.i("txttodb", "DBManager--oncreate");
		mDbHelper = new DatabaseHelper(context, "pathCode.db");
		//执行getWritableDatabase或者getReadableDatabase才能生成.db文件
		mDb = mDbHelper.getWritableDatabase();
	}
}

3、在MainActivity.java中调用

DBManager mDbManager = new DBManager(getApplicationContext());

三、把txt文件push进去databases



Android学习笔记(35)--- 使用sqlite 把.txt文件导进.db_第1张图片

四、txt内容导进表中

Android学习笔记(35)--- 使用sqlite 把.txt文件导进.db_第2张图片


五、打开.db查看是否导入数据成功

Android学习笔记(35)--- 使用sqlite 把.txt文件导进.db_第3张图片


你可能感兴趣的:(android,sqlite)