SQLite语句

1.重写SQLiteOpenHelper

// 例.
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
    public MySQLiteOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }
    /**
     * 重写的构造方法,使用预设库名
     * @param context
     */
    public MySQLiteOpenHelper(Context context){
        //上下文环境,数据库名,null,版本号(数据库版本号可随便写)
        super(context,"inet_address",null,2);
    }

    /**
     * 创建数据库方法,仅会调用一次
     * @param sqLiteDatabase
     */
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        //如果这个表存在则删除
        sqLiteDatabase.execSQL("drop table if exists t_main");
        //建表
        sqLiteDatabase.execSQL("CREATE TABLE t_main (s_id  varchar(20) primary key," +
                "s_i integer"+
                "s_inet_address text )");
    }

    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}

2.建表 

public void onCreate(SQLiteDatabase db) {
    db.execSQL("drop table if exists d_record");//如果这个表存在则删除

    db.execSQL("CREATE TABLE d_record (s_id  integer primary key," +
                " s_title varchar(20)," +
                " s_con text," +
                " s_uid varchar(20)," +//用户ID
                " s_time varchar(20) ,"+"s_type varch(20) )");
}

3.插入

SQLiteDatabase database_write=helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("s_name", name );
values.put("s_conn", conn );
values.put("s_imageId", imageId );
database_write.insert("d_record","s_id",values);
database_write.close();

insert()方法接收三个参数:表名、当插入行的其中一列为NULL时,数据将放置的列名(如果没有这样的列,则传入 null)和要插入的数据集合。 

4.查询

SQLiteDatabase sqLiteDatabase=new MySQLiteOpenHelper(MainActivity.this).getReadableDatabase();

Cursor cursor=sqLiteDatabase.query("t_main",new String[]{"inet_address"},null,null,null,null,null);

cursor.moveToNext();  //查询后得到Cursor,读取前要先移动指针

cursor.close();

sqLiteDatabase.close();

下面是query()方法的基本语法:

public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
  • table:指定要查询的数据库表的名称。
  • columns:指定要返回的列的名称,如果为null,则返回所有列。
  • selection:指定WHERE子句,用于过滤结果。可为null。
  • selectionArgs:指定selection参数中的占位符的值。可为null。
  • groupBy:指定分组的列。可为null。
  • having:指定HAVING子句,用于过滤分组。可为null。
  • orderBy:指定结果的排序方式。可为null。
  • limit:指定返回的行数限制。可不写。

5.更新

MySQLiteOpenHelper mySQLiteOpenHelper=new MySQLiteOpenHelper(InetAddressManagerActivity.this);
SQLiteDatabase sqLiteDatabase=mySQLiteOpenHelper.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put("s_inet_address","127.0.0.1");
sqLiteDatabase.update("t_main",contentValues,"id = ?",new String[]{"1"});
sqLiteDatabase.close();
mySQLiteOpenHelper.close();

update()方法用于更新数据库中的现有行。它接受四个参数: 

  • 表名称:要更新的表的名称。
  • ContentValues 对象:包含新值的列/值对。
  • WHERE 子句:确定要更新哪些行的条件子句。如果传入 null,则将更新表中的所有行。
  • WHERE 子句中的参数值数组:用于替换 WHERE 子句中的占位符的值。

tag:SQLite;数据库;查询;插入;更新;建表

  • 12
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Qt 中可以使用 QSqlQuery 类来执行 SQLite 数据库SQL 语句。下面是一些常用的 SQLite 语句及其 Qt 实现: 1. 创建表 ```sql CREATE TABLE table_name ( column1 datatype, column2 datatype, column3 datatype, ..... ); ``` ```c++ QSqlQuery query; query.exec("CREATE TABLE table_name (" "column1 datatype," "column2 datatype," "column3 datatype," "....." ")"); ``` 2. 插入数据 ```sql INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); ``` ```c++ QSqlQuery query; query.prepare("INSERT INTO table_name (column1, column2, column3, ...) VALUES (?, ?, ?, ...)"); query.addBindValue(value1); query.addBindValue(value2); query.addBindValue(value3); // ... query.exec(); ``` 3. 查询数据 ```sql SELECT column1, column2, column3, ... FROM table_name WHERE condition; ``` ```c++ QSqlQuery query; query.exec("SELECT column1, column2, column3, ... FROM table_name WHERE condition"); while(query.next()) { QString column1 = query.value(0).toString(); QString column2 = query.value(1).toString(); QString column3 = query.value(2).toString(); // ... } ``` 4. 更新数据 ```sql UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition; ``` ```c++ QSqlQuery query; query.prepare("UPDATE table_name SET column1 = ?, column2 = ?, ... WHERE condition"); query.addBindValue(value1); query.addBindValue(value2); // ... query.exec(); ``` 5. 删除数据 ```sql DELETE FROM table_name WHERE condition; ``` ```c++ QSqlQuery query; query.exec("DELETE FROM table_name WHERE condition"); ``` 以上就是一些常用的 SQLite 语句及其在 Qt 中的实现方式。需要注意的是,在执行 SQL 语句时,应该使用 prepare 和 addBindValue 函数来避免 SQL 注入攻击。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

在下嗷呜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值