一、新建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
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 } }
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(); } }
DBManager mDbManager = new DBManager(getApplicationContext());
四、txt内容导进表中
五、打开.db查看是否导入数据成功