VC + + Operation SQLite Simple instance _c language

Source: Internet
Author: User
Tags datetime sql error sqlite sqlite database

For many programmers, SQLite is no stranger. SQLite is a major open source database for embedded, can be integrated in its own desktop program, can also replace access as a background database. SQLite supports most SQL92 standards, such as indexing, throttling, triggering, and viewing support, support for NULL, INTEGER, Real, TEXT, and BLOB data types, and supports transactions. Here are some simple uses of SQLite.

Establish database
c:\sqlite-3_6_11> Sqlite3.exe dbname.db

When you set up the database, Sqlite3.exe followed by the database file name

Create a datasheet
sqlite> create TABLE users (userid varchar () PRIMARY KEY,
...> age int, ...>
birthday datetime);
Add records
INSERT into users values (' Wang ', ' 1989-5-4 ');
Insert into users values (' Li ', ' 1987-11-16 ');
Query record
SELECT * from the users order by birthday;
Delete Record Delete from
users where userid= ' Wang ';
Exit
sqlitesqlite>. Exit

The data structure of the SQLite database is stored in the "sqlite_master" table, and specific commands can be entered. Help view or refer to the SQLite assistance document.
A simple example of using SQLite under VC:

#include ". /sqlite3_lib/sqlite3.h "//Please refer to your address #pragma comment (lib,".) 
/sqlite3_lib/sqlite3.lib ")//please use your address as quasi static int _sql_callback (void * notused, int argc, char * * argv, char * * szcolname) {
int i; for (i=0 i < argc i++) {printf ("%s =%s\n", Szcolname[i], argv[i] = = 0?
"NUL": Argv[i]);
return 0; int main (int argc, char * argv[]) {const char * sSQL1 = ' CREATE TABLE users ' (userid varchar () PRIMARY KEY, age int, bi
Rthday datetime); ";
const char * sSQL2 = "INSERT into users values (' Wang ', 20, ' 1989-5-4 ');";
const char * sSQL3 = "SELECT * from users;";
Sqlite3 * db = 0;
char * perrmsg = 0;
int ret = 0;
Connection database ret = Sqlite3_open ("./test.db", &db);
if (ret!= sqlite_ok) {fprintf (stderr, "Cannot Open database:%s", sqlite3_errmsg (db)); return (1);} printf ("Database connection succeeded!\n");
Execute SQL Build Database sqlite3_exec (db, sSQL1, 0, 0, &perrmsg); if (ret!= sqlite_ok) {fprintf (stderr, "SQL Error:%s\n", perrmsg); Sqlite3_free (perrmsg);}//Insert record sqlite3_exec (db, S
SQL2, 0, 0, &perrmsg); CheckInquiry Data Sheet sqlite3_exec (db, sSQL3, _sql_callback, 0, &perrmsg);
Close Database Sqlite3_close (db);
db = 0;
return 0; }
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.