简体   繁体   English

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

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

The following is what I had tried on getting vector pushback and what I lack of now is to assign it 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;
}

Below is my database look on sqlite: 下面是我在sqlite上的数据库外观:

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

I want to get the value "admin" & "password" together through sql select statement and then assign them to a string, how do i use vector to obtain this element [0] and element [3] to a string. 我想通过sql select语句一起获取值“ admin”和“ password”,然后将它们分配给字符串,我如何使用向量将这个元素[0]和元素[3]获取到字符串。 as i need it for a if compareTo later on. 因为我以后需要if ifToTo。

Thanks for all the help !! 感谢所有的帮助!

You are already pushing strings into your vector of vectors. 您已经将字符串放入向量的向量中。 Since you have organized your vectors as each row representing the same column value, if you want the strings for record r , you would do: 由于您已经将向量组织为代表相同列值的每一行,因此,如果您想要记录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