Android里数据库的创建以及读写
public class DataProvider extends ContentProvider { private static final String Log_Tag = "CotentProvider"; private static final String DATABASE_NAME = "*.db"; private static final int DATABASE_VERSION = 2; private static final String TABLE__PEOPLE_NAME = "**";//表名 //重写SQLiteOpenHelper private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } //通过重写该函数来创建db和表,执行sql语句 public void onCreate(SQLiteDatabase db) { db.execSQL("Create TABLE " + TABLE__PEOPLE_NAME + " (" + "name" + " TEXT," + "chenghao" + " TEXT," + "shihao" + " TEXT" + ");"); db.execSQL("Create TABLE " + TABLE__SERIES_NAME + " (" + "name" + " TEXT," + "description" + " TEXT" + ");"); String sql_1 = "insert into "+ TABLE__PEOPLE_NAME + " (name, chenghao, shihao) values('****', '***', '***');"; try { db.execSQL(sql_1); } catch (SQLException e){ Log.e("ERROR", e.toString()); } } public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {} } private DatabaseHelper mOpenHelper; @Override public int delete(Uri uri, String selection, String[] selectionArgs) { // TODO Auto-generated method stub // SQLiteDatabase db = mOpenHelper.getWritableDatabase(); // for(String value:selectionArgs) // { // String sql = "delete from "+ TABLE_NAME + " where " + selection + "=" + value + ";"; // Log.d(Log_Tag, sql); // db.execSQL(sql); // } // Log.d(Log_Tag, selection); // for(String value:selectionArgs) // Log.d(Log_Tag, value); //db.delete(TABLE_NAME, selection, selectionArgs); return 0; } @Override public String getType(Uri uri) { // TODO Auto-generated method stub return null; } public Uri insert(Uri uri, ContentValues values) { // TODO Auto-generated method stub // SQLiteDatabase db = mOpenHelper.getWritableDatabase(); // db.insert(TABLE_NAME, null, values); return uri; } @Override public boolean onCreate() { // TODO Auto-generated method stub mOpenHelper = new DatabaseHelper(getContext()); return true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // TODO Auto-generated method stub SQLiteDatabase db = mOpenHelper.getWritableDatabase(); Cursor c; if (uri == PeopleList.CONTENT_URI) c = db.query(TABLE__PEOPLE_NAME, projection, null, null, null, null, null); else if (uri == PiliSeries.CONTENT_URI) c = db.query(TABLE__SERIES_NAME, projection, null, null, null, null, null); else c = db.query(TABLE__PEOPLE_NAME, projection, null, null, null, null, null); return c; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { // TODO Auto-generated method stub return 0; } }
写好的dataprovider要在manifest添加才能使用
<provider android:name="DataProvider" android:authorities="com.**.provider.SQ02"> </provider>
添加完后在需要查、加、删等对数据库操作的地方首先声明以下:
public static final String AUTHORITY = "com.**.provider.SQ02"; //和manifes一样的权限 public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/表名字"); private static final String[] PROJECTION = new String[] { "name", "chenghao", "shihao"}; //每列的名字
需要获得表里所有数据的参考code:
Intent intent = getIntent(); if (intent.getData() == null) intent.setData(CONTENT_URI); cur = getContentResolver().query(getIntent().getData(), PROJECTION, null, null, null); cur.moveToFirst(); while(!cur.isAfterLast()) { if(cur.getString(0) != ""){ //这里通过索引对每列的数据进行处理等操作 } cur.moveToNext(); }
插入操作我写的code是:
Intent intent = getIntent(); if (intent.getData() == null) intent.setData(CONTENT_URI); ContentValues values = new ContentValues(); values.put("列名", 值); values.put("列名", 值); getContentResolver().insert(CONTENT_URI, values);
删除
2 Android在sd卡里读写文件
1)声明权限使用许可
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2)获得sdcard文件路径
File sdDir = new File(Environment.getExternalStorageDirectory().getPath()); Log.d("FileTest", " " + sdDir.canWrite()); if (sdDir.exists() && sdDir.canWrite()){ Log.d("FileTest", sdDir.getAbsolutePath()); File testDir = new File(sdDir.getAbsolutePath() + "/test_folder"); testDir.mkdir(); } 这里有几种方法获得此文件(在Linux里都是文件,只是分文件夹和文件而已)
3)可以创建文件夹文件,读取文件,这和java里的操作差不多