菜鸟编程的数据库的增删改查(仅供参考)

Posted lin513

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了菜鸟编程的数据库的增删改查(仅供参考)相关的知识,希望对你有一定的参考价值。

public class MainActivity extends AppCompatActivity implements View.OnClickListener 

    private Context mContext;
    private Button btn_insert;
    private Button btn_query;
    private Button btn_update;
    private Button btn_delete;
    private SQLiteDatabase db;
    private MyDBOpenHelper myDBHelper;
    private StringBuilder sb;
    private int i = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = MainActivity.this;
        myDBHelper = new MyDBOpenHelper(mContext, "my.db", null, 1);
        bindViews();
    

    private void bindViews() 
        btn_insert = (Button) findViewById(R.id.btn_insert);
        btn_query = (Button) findViewById(R.id.btn_query);
        btn_update = (Button) findViewById(R.id.btn_update);
        btn_delete = (Button) findViewById(R.id.btn_delete);

        btn_query.setOnClickListener(this);
        btn_insert.setOnClickListener(this);
        btn_update.setOnClickListener(this);
        btn_delete.setOnClickListener(this);
    

    @Override
    public void onClick(View v) 
        db = myDBHelper.getWritableDatabase();
        switch (v.getId()) 
            case R.id.btn_insert:
                ContentValues values1 = new ContentValues();
                values1.put("name", "呵呵~" + i);
                i++;
                //参数依次是:表名,强行插入null值得数据列的列名,一行记录的数据
                db.insert("person", null, values1);
                Toast.makeText(mContext, "插入完毕~", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_query:
                sb = new StringBuilder();
                //参数依次是:表名,列名,where约束条件,where中占位符提供具体的值,指定group by的列,进一步约束
                //指定查询结果的排序方式
                Cursor cursor = db.query("person", null, null, null, null, null, null);
                if (cursor.moveToFirst()) 
                    do 
                        int pid = cursor.getInt(cursor.getColumnIndex("personid"));
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        sb.append("id:" + pid + ":" + name + "\\n");
                     while (cursor.moveToNext());
                
                cursor.close();
                Toast.makeText(mContext, sb.toString(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_update:
                ContentValues values2 = new ContentValues();
                values2.put("name", "嘻嘻~");
                //参数依次是表名,修改后的值,where条件,以及约束,如果不指定三四两个参数,会更改所有行
                db.update("person", values2, "name = ?", new String[]"呵呵~2");
                break;
            case R.id.btn_delete:
                //参数依次是表名,以及where条件与约束
                db.delete("person", "personid = ?", new String[]"3");
                break;
        
    

数据的增删改查(三层)

      进行数据操作必然少了对数据的增删改查,用代码生成器生成的代码不是那么满意!方便在今后使用,这里就主要写“数据访问层(Dal)”

注:这里由于是用于用于测试时,临时建的数据库用于测试使用,在实际使用过程中些许改点参数就可以使用了

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(long Id)
        {
            string sql = "select count(*) Name from t_temp where [email protected]";
            MySqlParameter[] param = { new MySqlParameter("@id", Id) };
            long data=(long)MySqlHelper.ExecuteScalar(sql, param);
            if (data > 0)
            {
                return true;
            }        
            return false;
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public long Add(Model model)
        {
            string sql = "insert into t_temp (Name) values (@name)";
            MySqlParameter[] param = { new MySqlParameter("@name", model.Name) };
            return MySqlHelper.ExecuteNonQuery(sql, param);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model model)
        {
            string sql = "Update t_temp set [email protected] where [email protected]";
            MySqlParameter[] param = { new MySqlParameter("@name", model.Name), new MySqlParameter("@id", model.Id) };
            if (MySqlHelper.ExecuteNonQuery(sql, param) > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(long Id)
        {
            string sql = "delete from t_temp where [email protected]";
            MySqlParameter[] param = { new MySqlParameter("@id", Id) };
            if (MySqlHelper.ExecuteNonQuery(sql, param) > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            string sql = "delete from t_temp where ID in (" + Idlist + ")";
            if (MySqlHelper.ExecuteNonQuery(sql) > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model GetModel(long Id)
        {
            string sql = "select ID,Name from t_temp where [email protected]";
            MySqlParameter[] param = { new MySqlParameter("@id", Id) };
            DataTable table= MySqlHelper.ExecuteQuery(sql, param);
            if (table != null)
            {
                if (table.Rows.Count > 0)
                {
                    return DataRowToModel(table.Rows[0]);
                }
            }
            return null;          
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model DataRowToModel(DataRow row)
        {
            Model mod = new Model();
            if (row != null)
            {
                if(!string.IsNullOrEmpty(row["ID"].ToString()))
                {
                    mod.Id=long.Parse(row["ID"].ToString());
                }
                if (!string.IsNullOrEmpty(row["Name"].ToString()))
                {
                    mod.Name = row["Name"].ToString();
                }
            }
            return mod;
        }

 

 

 

以上是关于菜鸟编程的数据库的增删改查(仅供参考)的主要内容,如果未能解决你的问题,请参考以下文章

mybatis生成的增删改查怎么用

java web 就是对数据库的增删改查么

求ACCESS数据库的增删改语句

数据的增删改查(三层)

vue-实现对数组的增删改查

mysql的增删改查语句