SQLite database beginners

Source: Internet
Author: User
Tags sql error

Just a few days after I learned C ++, I had to use the database for the program, and I didn't want to use the control of MFC or something that I hadn't studied yet. I found an open-source small program that can be embedded.

Type database SQLite (home http://www.sqlite.org ).

Download sqlite-3_2_2.zip (

A command line sqlitedemo program example and sqlitedll-3_2_2.zip (a DLL ).
First, run the command line SQLite demo program, according to the document provided by him (a simple tutorial ).

I use commands
". Output F: \ programming \ database \ SQLite \ sqlite-3_2_2 \ E"
A file named "programming Database SQlitesqlite-3_2_2e" is generated under the SQLite working directory, but I want to generate it under c: \ test \

When the file is converted, he reports the following error:

SQLite>. Output C: \ test \ 1
Can't write to "C: est?

I don't know why.

Use

SQLite> Create Table tbl1 (one varchar (10), two smallint );
SQLite> insert into tbl1 values ('Hello! ', 10 );
SQLite> insert into tbl1 values ('Goodbye ', 20 );
SQLite> select * From tbl1;

Create a new table and add two data items.
Hello! | 10
Goodbye | 20

The following is a tutorial I found on Google.

Learning SQLite in less than five minutes
Translator: wide288

This shows you how to experiment with SQLite. There is no long description and configuration here. :

Download Code
Obtain a binary copy or source code and compile it by yourself. For more information about the download page.

Create a new database
Enter "sqlite3 test. DB" in the DOS command line to create a new database file named "test. DB". (You can use different names.

Characters)

Enter the SQL command to create and write new data at the prompt.

Write a program using SQLite
The following is a simple Tcl program. Let's see how to use the Tcl interface of SQLite. This program executes the second parameter execution of the SQL statement.

SQL command. This command is sqlite3 to open an SQLite database and create a new TCL command in line 7th, the name is "DB"

After receiving the database, the database command executes the SQL command on the 8th line to the database and closes the connection to the database.

#! /Usr/bin/tclsh
If {$ argc! = 2 }{
Puts stderr "Usage: % s Database SQL-STATEMENT"
Exit 1
}
Load/usr/lib/tclsqlite3.so sqlite3
Sqlite3 DB [lindex $ argv 0]
DB eval [lindex $ argv 1] X {
Foreach v $ X (*){
Puts "$ v = $ X ($ v )"
}
Puts ""
}
DB close

The following example shows how to use the C/C ++ interface of SQLite. The database name is obtained by the first parameter and the second parameter or

More parameters are SQL Execution statements. This function calls sqlite3_open () to open the database in 22 rows, and sqlite3_exec () runs on 27 rows.

Run the SQL command and sqlite3_close () to close the database connection on line 31.

# Include <stdio. h>
# Include <sqlite3.h>

Static int callback (void * notused, int argc, char ** argv, char ** azcolname ){
Int I;
For (I = 0; I <argc; I ++ ){
Printf ("% s = % s \ n", azcolname [I], argv [I]? Argv [I]: "null ");
}
Printf ("\ n ");
Return 0;
}

Int main (INT argc, char ** argv ){
Sqlite3 * dB;
Char * zerrmsg = 0;
Int RC;

If (argc! = 3 ){
Fprintf (stderr, "Usage: % s Database SQL-STATEMENT \ n", argv [0]);
Exit (1 );
}
Rc = sqlite3_open (argv [1], & dB );
If (RC ){
Fprintf (stderr, "can't open database: % s \ n", sqlite3_errmsg (db ));
Sqlite3_close (db );
Exit (1 );
}
Rc = sqlite3_exec (dB, argv [2], callback, 0, & zerrmsg );
If (RC! = Sqlite_ OK ){
Fprintf (stderr, "SQL error: % s \ n", zerrmsg );
}
Sqlite3_close (db );
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.