sqlitedabase提供的CRUD方法

Posted Veer Han

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sqlitedabase提供的CRUD方法相关的知识,希望对你有一定的参考价值。

1.mysqliteOpenHelper.java

public class MySQLiteOpenHelper extends SQLiteOpenHelper 

    private static String name = "hytc.db";

    private static int version = 1;

    public MySQLiteOpenHelper(Context context) 
        super(context, name, null, version);
        // TODO Auto-generated constructor stub
    

    @Override
    public void onCreate(SQLiteDatabase db) 
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE IF NOT EXISTS user (_id integer primary key autoincrement, name varchar(20), phone varchar(11))");
    

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
        // TODO Auto-generated method stub

    

2.DemoTest.java

public class DemoTest extends androidTestCase 

    private MySQLiteOpenHelper helper;

    public void testinsert() 
        // 1.创建数据库管理对象
        helper = new MySQLiteOpenHelper(getContext());
        // 2.获取数据库对象
        SQLiteDatabase db = helper.getWritableDatabase();
        for (int i = 1; i < 40; i++) 
            // 创建集合对象
            ContentValues values = new ContentValues();
            values.put("name", "chj" + i);
            values.put("phone", "110" + i);
            // 3.保存数据
            long id = db.insert("user", "phone", values);
            System.out.println(id);

        
        db.close();
    

    public void testupdate() 
        // 1.创建数据库管理对象
        helper = new MySQLiteOpenHelper(getContext());
        // 2.获取数据库对象
        SQLiteDatabase db = helper.getWritableDatabase();

        // 创建集合对象
        ContentValues values = new ContentValues();
        values.put("name", "chjx");
        values.put("phone", "110");
        // 3.保存数据
        long id = db.update("user", values, "_id=?", new String[]  2 + "" );
        System.out.println(id);

        db.close();

    

    public void testdelete() 
        // 1.创建数据库管理对象
        helper = new MySQLiteOpenHelper(getContext());
        // 2.获取数据库对象
        SQLiteDatabase db = helper.getWritableDatabase();
        // 3.保存数据
        long id = db.delete("user", "_id=?", new String[]  2 + "" );
        System.out.println(id);

        db.close();

    

    public void testquery() 
        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
        // 1.创建数据库管理对象
        helper = new MySQLiteOpenHelper(getContext());
        // 2.获取数据库对象
        SQLiteDatabase db = helper.getWritableDatabase();
        // 3.查询数据
        Cursor c = db.query("user", new String[]  "_id", "name", "phone" ,
                null, null, null, null, "_id desc", "1,2");

        // 4.循环遍历
        while (c.moveToNext()) 
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("_id", c.getInt(c.getColumnIndex("_id")));
            map.put("name", c.getString(c.getColumnIndex("name")));
            map.put("phone", c.getString(c.getColumnIndex("phone")));
            data.add(map);
        

        c.close();
        db.close();
        for (Map p : data) 
            System.out.println(p.get("name"));
        
    

以上是关于sqlitedabase提供的CRUD方法的主要内容,如果未能解决你的问题,请参考以下文章

Flutter CRUD 模型与提供者

java oop第09章_JDBC02(CRUD操作)

Mybatis-Plus通用Mapper CRUD之insert(附带视频教程)

与json-server对接,为组件提供CRUD API功能

MyBatis3系列__04CRUD以及参数处理

阶段3 1.Mybatis_05.使用Mybatis完成CRUD_2 Mybatis的CRUD-保存操作