zoukankan      html  css  js  c++  java
  • Using SQLite in android demo

    1.in android,you can using SQLite save data,as flowing exampe:

    package tuo.test;
    
    import tuo.test.entity.UserProfile;
    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    
    public class GucSQLite extends SQLiteOpenHelper {
    
    	private static final String DATABASE_NAME = "GucNightingale.db";
    	private static final String TABLE_NAME = "nurse_app_setting";
    	private static final int DATABASE_VERSION = 1;
    
    	String TAG = "GucSQLite";
    
    	public GucSQLite(Context context) {
    		super(context, DATABASE_NAME, null, DATABASE_VERSION);
    	}
    
    	@Override
    	public void onCreate(SQLiteDatabase database) {
    		String creat_sql = "CREATE TABLE "
    				+ TABLE_NAME
    				+ " (id INTEGER PRIMARY KEY AUTOINCREMENT,mobile TEXT,appid TEXT,pwd TEXT,dept TEXT,his_account TEXT)";
    		database.execSQL(creat_sql);
    	}
    
    	@Override
    	public void onUpgrade(SQLiteDatabase database, int oldVersion,
    			int newVersion) {
    		database.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
    		onCreate(database);
    	}
    
    	public UserProfile getUserProfile() {
    		UserProfile userProfile = null;
    		SQLiteDatabase db = this.getReadableDatabase();
    		Cursor cursor = db.query(TABLE_NAME, new String[] { "id", "appid","mobile", "pwd", "dept", "his_account" }, null, null, null,null, null);
    		while (cursor.moveToNext()) {
    			userProfile = new UserProfile();
    			userProfile.setId(cursor.getString(0));
    			userProfile.setAppid(cursor.getString(1));
    			userProfile.setMobile(cursor.getString(2));
    			userProfile.setPwd(cursor.getString(3));
    			userProfile.setDept(cursor.getString(4));
    			userProfile.setHis_account(cursor.getString(5));
    		}
    		return userProfile;
    	}
    
    	public boolean saveOrUpdateUserProfile(UserProfile user) {
    		int result;
    		ContentValues values = new ContentValues();
    		values.put("appid", user.getAppid());
    		values.put("mobile", user.getMobile());
    		values.put("pwd", user.getPwd());
    		values.put("dept", user.getDept());
    		values.put("his_account", user.getHis_account());
    		if (isExist(user)) {
    			String whereClause = "mobile =?";
    			String[] whereArgs = new String[] { user.getMobile() };
    			result = (int) this.getWritableDatabase().update(TABLE_NAME,values, whereClause, whereArgs);
    		} else {
    			result = (int) this.getWritableDatabase().insertOrThrow(TABLE_NAME,null, values);
    		}
    		return result > 0;
    	}
    
    	private boolean isExist(UserProfile user) {
    		return this.getReadableDatabase()
    				   .rawQuery("SELECT id FROM " + TABLE_NAME + " WHERE mobile="+ user.getMobile(), null).getCount() > 0;
    	}
    
    }
    
  • 相关阅读:
    Different AG groups have the exactly same group_id value if the group names are same and the ‘CLUSTER_TYPE = EXTERNAL/NONE’
    An example of polybase for Oracle
    use azure data studio to create external table for oracle
    Missing MSI and MSP files
    You may fail to backup log or restore log after TDE certification/key rotation.
    Password is required when adding a database to AG group if the database has a master key
    Use KTPASS instead of adden to configure mssql.keytab
    ardunio+舵机
    android webview 全屏100%显示图片
    glide 长方形图片显示圆角问题
  • 原文地址:https://www.cnblogs.com/tuolin/p/2184288.html
Copyright © 2011-2022 走看看