C + + Get sqlite3 of all the fields in a database table summary _c language

Source: Internet
Author: User
Tags parse string sqlite

Common methods:

1. Using the Sqlite3_get_table function

2. Get SQLite to create the SQL statement string for the table, and then resolve to get to the appropriate field

3. Use the configuration file to write all field names to the configuration file

The following three methods to give you a detailed description.

Method 1: Use the sqlite3_get_table function

Code:

Char *dbname = "test.db";
int rc = Sqlite3_open (dbname, &db);
if (rc = = SQLITE_OK)
{
char sql[256] = "SELECT * from Images";
char** PResult;
int nrow;
int ncol;
rc = sqlite3_get_table (db, SQL, &presult, &nrow, &ncol, NULL);
if (rc = = SQLITE_OK)
{
if (nrow >= 1) {
...}}
}
Sqlite3_free_table (PResult);
}
Sqlite3_close (DB);

Analysis:

Nrow returns the number of rows (excluding the field name) of the record that was queried, the number of columns returned by Ncol, and all returned data, including all field names, can be accessed by presult. The following figure:

Nrow=7,ncol=6, presult[0-5] Returns the name of the field in the current table, and each of the field names is an array of strings.

The disadvantage of this method is that if a record does not exist in the current table, PRESULT[0-NCOL-1 will not be accessible so that the field in the table cannot be obtained. In practical applications, it is often necessary to get all the field names in the table for later insert deletion and update after the program has just started, so the method is not practical.

Method 2: Get SQLite SQL statement string to create table

Process Analysis: sqlite3 A table is automatically generated after a database is created and the corresponding table is formatted as follows:

Where the field SQL is a CREATE statement that creates a table, you can get the CREATE SQL statement for the specified table by sqlite3_get_table, and then parse the string to get all the field names in the specified table.

Code:

Gets the Create SQL statement string Db_getfigurecreatesql () {String res = "" In the figures table above;
int rc = Sqlite3_open (dbname, &db); if (rc = = SQLITE_OK) {Char *sql = "Select SQL from sqlite_master where tbl_name = ' figures ' and type = ' table '"; char** p
result;
int nrow;
int ncol;
rc = sqlite3_get_table (db, SQL, &presult, &nrow, &ncol, NULL);
if (rc = = SQLITE_OK) {if (Nrow >= 1) {res = Presult[ncol];} sqlite3_free_table (PResult);
} sqlite3_close (db);
return res; }//Parse String function std::vector<string> split (string str, string separator) {std::vector<string> result; int cutat; w 
Hile ((Cutat = str.find_first_of (separator))!= Str.npos) {if (Cutat > 0) {result.push_back (str.substr (0, Cutat));}
str = STR.SUBSTR (cutat + 1);
} if (Str.length () > 0) {result.push_back (str); //Get the field name in the table std::vector<string> Db_getkeyfieldname (string res) {std::vector<string> r = Split (res, ","); STD
::vector<string> Keyfield; for (int i = 0; i < r.size (); ++i) {std::vector<string> TP = Split (R[i], "\"); if (tp.size () = = 2) {keyfield.push_back (tp[0]);} else if (tp.si
Ze () = = 3) {keyfield.push_back (tp[1]);}
return Keyfield; }

How to use

std::vector<string> FieldName = Db_getkeyfieldname (Db_getfigurecreatesql ());

Result Analysis: If a new field can be added at any time in the table, add the field's SQL statement as follows:

ALTER TABLE x ADD COLUMN "New_col",

Where New_col is the field name, double quotes must be added because the parsed procedure is delimited by double quotes "\". (Note: double quotes in SQL statements, single quotes, or no quotes can be used to add fields to a specified table successfully)

Method 3: Use a configuration file to write all field names to the configuration file

For some concrete and complex scenarios, you can use the configuration file to write all field names to the corresponding configuration file, each time you enter the application, read the configuration file. Also, if some fields have optional values in the specified, these optional values can also be written to the configuration file. I use the nonstandard configuration file to do not share, the recent study of the configuration

The specification of the document.

The above is a small set to introduce C + + to get sqlite3 database table all the fields of the method summary, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.