繁体   English   中英

如何使用select语句检索sqlite并分配给vector然后分配给字符串

[英]How to retrieve sqlite with select statement and assign to vector then assign to a string

以下是我尝试获取向量推回的尝试,现在缺少的是将其分配给字符串:

#include <iostream>
#include <sqlite3.h>
#include <vector>

using namespace std;

int main()
{

    sqlite3 *db;
    sqlite3_stmt * stmt;
    std::vector< std::vector < std:: string > > result;
    for( int i = 0; i < 4; i++ )
        result.push_back(std::vector< std::string >());

    if (sqlite3_open("abeserver.db", &db) == SQLITE_OK)
    {
        sqlite3_prepare( db, "SELECT * from abe_account;", -1, &stmt, NULL );
                              //preparing the statement
        sqlite3_step( stmt ); //executing the statement

        while( sqlite3_column_text( stmt, 0 ) )
        {
            for( int i = 0; i < 4; i++ )
                result[i].pushback( std::string( (char *)sqlite3_column_text( st, i ) ) );
            sqlite3_step( stmt );
        }

    }
    else
    {
        cout << "Failed to open db\n";
    }

    sqlite3_finalize(stmt);
    sqlite3_close(db);

    return 0;
}

下面是我在sqlite上的数据库外观:

sqlite> select * from abe_account;
admin|Peter John|admin_account|password

我想通过sql select语句一起获取值“ admin”和“ password”,然后将它们分配给字符串,我如何使用向量将这个元素[0]和元素[3]获取到字符串。 因为我以后需要if ifToTo。

感谢所有的帮助!

您已经将字符串放入向量的向量中。 由于您已经将向量组织为代表相同列值的每一行,因此,如果您想要记录r的字符串,则可以执行以下操作:

std::string username = result[0][r];
std::string password = result[3][r];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM