C language Use SQLITE3 database "section III"

Source: Internet
Author: User
Tags sqlite

int sqlite3_get_table (  sqlite3 *db,/          * Database handle */  const char *zsql,/     * SQL statement */  Char ***pazresult,
     /* query results A large array of one-dimensional arrays *  /int *pnrow,/           * Number of rows */  int *pncolumn,/        * Number of fields that's how many columns but the data is also based on this. For example: First line: 1 , 2,3 then this value defaults to 3, because the first row is the field name, the natural is the number of columns, then how to take the individual data bit?                           A: Starting from the next line for example, 4 is the first data, and then the left-to-right movement, the value changes, the formation of data positioning! Personal understanding, the great God do not spray! *  /char **pzerrmsg/       * error message same as previous errmsg */); sqlite3_free_table (char **result);//release cache of queries

Then please look at the following source code comments:

# include <stdio.h>
# include "Sqlite3.h"
# include <stdlib.h>
int main (void)
{
int i;
Int J;
Sqlite3 *db;//establishes a sqlite3 type of pointer db! can act as a handle to the database.
Without this handle, all sqlite3 functions have no real meaning.
const char *sql1= "INSERT into SJK values (1, ' myd ', ' 2013-10-9 ');"; /assign a SQL statement to a variable!
const char *sql2= "INSERT into SJK values (2, ' myd ', ' 2013-10-9 ');"; /sql statement I will not say more.
Querying data from a database
const char *sql3= "SELECT * from SJK;"; /Here is the point where we can see the use of the callback function at the bottom!
Char *errmsg=0;
char** PResult;
int nrow;
int ncol;
int Feedback = Sqlite3_open ("sqlite3.db", &db);
Opens the specified database file if it does not exist, creates a database file with the same name, and returns the value to the DB handle.
if (Feedback! = SQLITE_OK)
{
printf ("The database named: sqlite3.db is not open \ nthe reason is:%s\n", sqlite3_errmsg (db));
System ("pause");
Sqlite3_errmsg () is the prompt message after returning an error! The parameter is the operation handle of the database.
Sqlite3_close (DB);
Sqlite3_close () is the database that closes the specified database handle!
Exit (-1);//non-normal exit!
}
else printf ("successfully opened or successfully created named: Sqlite3.db!\n");
Sqlite3_exec (DB, "CREATE TABLE SJK (ID integer,name text,birthday blob)", 0,0,&errmsg);//CREATE TABLE
Sqlite3_exec (db,sql1,0,0,&errmsg);//Insert Data 1
Sqlite3_exec (db,sql2,0,0,&errmsg);//Insert Data 2
printf ("Insert data successfully \ n");
Feedback = sqlite3_get_table (db,sql3,&presult,&nrow,&ncol,&errmsg);//query data returns data to &presult
int nIndex = Ncol; I did comment parsing before this article started, check it out, and then analyze why?
printf ("Total%d columns/n", nIndex);//Find out the default number of fields equals the number of columns
for (i=0;i<nrow;i++)//Query the first few lines of the loop
{
for (j=0;j<ncol;j++)//Query The data loop inside a row
{
printf ("%s=%s\n", Presult[j],presult[nindex]);
++nindex;//Locate the next data so that the field name matches!
}
}
Sqlite3_free_table (PResult);//Release memory
Sqlite3_close (db);//Close Database
db = 0;//Clear database handle
printf ("Database shutdown succeeded!") \ n ");
System ("pause");//Pause dos for easy viewing
return 0;
}

Then the basics of this tutorial will be here!
However, some people ask me, why do I always use source code and comments to explain it to you from the second class?
Nor is it exaggerated, I ask you, if you do not learn SQL statements will also consider which platform SQLite applies to?
If you do not have C language, then you will also use C language to manipulate SQLite? So many basic questions I will not explain to you.
So the next chapter, I will explain the intermediate article for you. To use it to do some DOS word game!

C language Use SQLITE3 database "section III"

Related Article

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.