如何使用模板生成 sqlite3 sql 创建语句?

时间:2021-12-11 05:22:53
template<typename T,typename... Args>
std::string createTable(T tableName,Args&&... args)
{
    std::string sql = "CREATE TABLE IF NOT EXISTS";
    sql += " ";
    sql += tableName;
    sql += "(";
    for(const auto &arg:{args...}){
        sql += arg;
        sql += ",";
    }
    if(!sql.empty()){
        char& back = sql.back();
        back = ')';
    }
    sql += ";";
    return sql;
}

    QString sql = QString::fromStdString(createTable(std::string("blog"),
                "id TEXT PRIMARY KEY NOT NULL",
                "title TEXT NOT NULL",
                "content TEXT NOT NULL",
                "push_time TEXT NOT NULL",
                "tag TEXT"));
    qDebug() << sql;
SELECT,INSERT,UPDATE,DELETE,ALTER TABLE.

Sqlite3 data type   SQL type description                                        Recommended input(C++ or Qt data type)
NULL                NULL value                                                  NULL
INTEGER             Signed integer,stored in 8,16,24,32,48,                     typedef qint8/16/32/64
                    or 64-bits depending on the magnitude of the value
REAL                64-bit floating point value                                 By default mapping to QString
TEXT                Character string(UTF-8,UTF-16BE or UTF-16-LE)               Mapped to QString
CLOB                Charater large string object                                Mapped to QString
BLOB                The value is a BLOB of data,stored exactly as it was input  Mapped to QByteArray