龙空技术网

做个不需要网络的离线账本APP

唐太重 73

前言:

现在你们对“rawquery”都比较注意,朋友们都需要学习一些“rawquery”的相关资讯。那么小编在网络上网罗了一些对于“rawquery””的相关资讯,希望小伙伴们能喜欢,同学们快快来了解一下吧!

android 的sqlite数据库平时用的不多,国庆也没事做,早上起来打算结合sqlite做个简单的账本APP,也顺便熟悉一下sqlite的相关功能。

sqlite助手类

public class DatabaseUtils {    private Context context;    public SQLiteDatabase mSQLiteDatabase = null;    private DatabaseHelper mDatabaseHelper = null;    private String dbName;    public DatabaseUtils(Context context,String dbName){        this.context = context;        this.dbName = dbName;    }    class DatabaseHelper extends SQLiteOpenHelper{        public DatabaseHelper(Context context, String dbName, SQLiteDatabase.CursorFactory factory, int version){            super(context,dbName,factory,version);        }        @Override        public void onCreate(SQLiteDatabase sqLiteDatabase) {            String sql = "create table account(_id INTEGER PRIMARY KEY AUTOINCREMENT,acid varchar(50),price int,remark varchar(255),type varchar(20),time varchar(20))";            sqLiteDatabase.execSQL(sql);        }        @Override        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {        }    }    public  void open(){        mDatabaseHelper = new DatabaseHelper(context,dbName,null,1);        mSQLiteDatabase = mDatabaseHelper.getWritableDatabase();    }    // 关闭数据库    public void close() {        mDatabaseHelper.close();    }    public long insert(String tabName, HashMap<String,String> param){            ContentValues cv = new ContentValues();            for (String key:param.keySet()){                cv.put(key,param.get(key));            }            return mSQLiteDatabase.insert(tabName, null, cv);    }    public Cursor select(String sql,String[] bindArgs){            return mSQLiteDatabase.rawQuery(sql, bindArgs);    }    public Cursor query(String tableName, String sqlString) {        synchronized (mDatabaseHelper) {            SQLiteDatabase database = mDatabaseHelper.getReadableDatabase();            Cursor cursor = database.rawQuery("select * from " + tableName + " " + sqlString, null);            return cursor;        }    }    public void querySql(String sql){             mSQLiteDatabase.execSQL(sql);    }    public void update(String tabName,HashMap<String,String> param,String whereClause, String[] whereArgs){        ContentValues cv = new ContentValues();        for (String key:param.keySet()){            cv.put(key,param.get(key));        }        mSQLiteDatabase.update(tabName,cv,whereClause,whereArgs);    }    public int delete(String tabName,String whereClause, String[] whereArgs){        return   mSQLiteDatabase.delete(tabName, whereClause, whereArgs);    }

记账界面XML

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=";    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="vertical"    android:padding="10dp"    >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center_vertical">        <TextView            android:layout_width="100dp"            android:layout_height="50dp"            android:textSize="20dp"            android:gravity="center_vertical"            android:text="类型:"/>         <Spinner             android:id="@+id/id_type"             android:layout_width="0dp"             android:layout_weight="1"             android:layout_height="wrap_content"/>    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content">        <TextView            android:layout_width="100dp"            android:layout_height="50dp"            android:textSize="20dp"            android:gravity="center_vertical"            android:text="金额:"/>        <EditText            android:id="@+id/id_price"            android:layout_width="0dp"            android:layout_weight="1"            android:inputType="number"            android:layout_height="50dp"            android:hint="请输入金额"/>    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:gravity="center_vertical"        android:layout_height="wrap_content">        <TextView            android:layout_width="100dp"            android:layout_height="50dp"            android:textSize="20dp"            android:gravity="center_vertical"            android:text="时间:"/>        <TextView            android:id="@+id/id_time"            android:layout_width="0dp"            android:layout_weight="1"            android:gravity="right"            android:layout_height="wrap_content"/>        <ImageView            android:id="@+id/id_select_date"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@mipmap/date"/>    </LinearLayout>    <EditText        android:id="@+id/id_remark"        android:textSize="20sp"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="请输入备注内容"/>    <Button        android:id="@+id/id_add"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="确定"/></LinearLayout>

界面效果:

标签: #rawquery