user1578897
user1578897

Reputation: 179

SQLite - How to use it at terminal level & C++ application?

i am new to sqlite and I just recently installed it. I am familar with mysql but I need to use sqlite as I am using it for a C++ application that I am going to create.

Question 1:

I type this at my command line terminal

root@ubuntu:/home/baoky/version1.2/Assignment 2# sqlite abeserver.db

then I saw this output

sqlite>

So i type .h and i see a list of help commands

But i wanna create a table

sqlite> .databases

seq  name             file                                                      
---  ---------------  ----------------------------------------------------------
0    main             /home/baoky/version1.2/Assignment 2/abeserver.db          
1    temp             /var/tmp/sqlite_hjT3FEefcAHRPhn 

in my main database

How do i execute this sql command at terminal level

CREATE TABLE abe_account (
  username TEXT,
  name TEXT,
  department TEXT,
  password TEXT
);

Question 2:

How do I insert record into the table abe_account using C++

Question 3:

How do I retrieve records from table abe_account and assign it to a string using C++

Sorry I tried google around and search around stack overflow, I am still confused with the usage, if its mysql, it would be much simple for me.

Upvotes: 0

Views: 513

Answers (3)

SingerOfTheFall
SingerOfTheFall

Reputation: 29966

If you are using the sqlite terminal, you can just type SQL there, and it will be executed.

A typical cycle of work from your C++ code will look something like this:

sqlite3 * db;//database
sqlite3_stmt * stmt;//sql statement

sqlite3_open( "database.db", & db );//opening database

sqlite3_prepare( db, "SELECT something FROM something else;", -1, &stmt, NULL );//preparing the statement
sqlite3_step( stmt );//executing the statement

while( sqlite3_column_text( stmt, 0 ) )
{
char * str = (char *) sqlite3_column_text( stmt, 0 );///reading the 1st column of the result
//do your stuff
sqlite3_step( stmt );//moving to the next row of the result
}

sqlite3_finalize(stmt);
sqlite3_close(db);

You can easily google the functions to learn about their arguments and what they do in-detail.

Upvotes: 1

user647772
user647772

Reputation:

To create a new database, just connect to it:

$ sqlite3 your_database_file

This will create your database in the file your_database_file. If this file already exists, the command will open it.

Then you can execute CREATE TABLE or any other SQL.

Upvotes: 1

arrowd
arrowd

Reputation: 34401

Question 2:

Question 3:

Let me google it for you, friend: An Introduction To The SQLite C/C++ Interface.

Upvotes: 2

Related Questions