Open, close, and create a database-SQLite tutorial (4)

Source: Internet
Author: User
Tags sql error sqlite tutorial
Statement

You are welcome to repost this article, but please respect the author's Labor achievements. repost this article and keep the statement in this box. Thank you.
Article Source: http://blog.csdn.net/iukey

At the end of the last lecture, I had some suspense. I don't know if you have any questions after reading the previous lecture. My question is that I have created a handle, but how do I know which database file the handle points to on the disk? We just created a pointer pointing to a struct of the sqlite3 type. The data in it is empty or default. What we need to do next is to apply for memory for this struct and fill it. Do you feel scared again? This structure is so huge that we can fill in the monkey year and month that we don't have yet. We don't need to worry about everything. We don't need to fill it explicitly. We just need to tell it some basic information, and then call the API to let the API fill for us.

Currently, we only need to inform this struct of the path of the database file. Then you can call the sqlite3_open function.

Sqlite_api int sqlite3_open (// sqlite_api is a macro used to mark the const char * zfilename, sqlite3 ** ppdb) of the API );

The first parameter is the path, const char * type, and the second parameter is the pointer of the database handle. Note that it is a second-level pointer. We declare the first-level pointer, therefore, we also need to add an access token &. Please refer to the following example:

int ret = sqlite3_open( getFilePath(), &pdb);//

Don't be surprised. In fact, opening a database is to get some information about the database, so that we can easily manipulate it, isn't it? Through this function, we bind the database with the handle PDB, and we will use this PDB to manipulate the database. In this function, the first parameter is the path of the database file (including the file name). I generally write the path as a function, this follows the encoding principle (the principle used here is to do only one thing for a function as much as possible). It is recommended that you do the same, so that you can gradually understand the benefits. You can refer to the following functions for obtaining paths:

Const char * getfilepath () {return [[nsstring stringwithformat: @ "% @/documents/DB", nshomedirectory ()] utf8string]; // sorry, here we use something related to iOS, but this is not feasible. Unless I write an absolute path, it doesn't work. // it doesn't matter either, you must also change the path when porting it to another place, so you can just change it when porting it}

Another point is the return value, which is used to determine whether execution is successful. SQLite
We have defined a series of macros as return values:

#define SQLITE_OK           0   /* Successful result *//* beginning-of-error-codes */#define SQLITE_ERROR        1   /* SQL error or missing database */#define SQLITE_INTERNAL     2   /* Internal logic error in SQLite */#define SQLITE_PERM         3   /* Access permission denied */#define SQLITE_ABORT        4   /* Callback routine requested an abort */#define SQLITE_BUSY         5   /* The database file is locked */#define SQLITE_LOCKED       6   /* A table in the database is locked */#define SQLITE_NOMEM        7   /* A malloc() failed */#define SQLITE_READONLY     8   /* Attempt to write a readonly database */#define SQLITE_INTERRUPT    9   /* Operation terminated by sqlite3_interrupt()*/#define SQLITE_IOERR       10   /* Some kind of disk I/O error occurred */#define SQLITE_CORRUPT     11   /* The database disk image is malformed */#define SQLITE_NOTFOUND    12   /* Unknown opcode in sqlite3_file_control() */#define SQLITE_FULL        13   /* Insertion failed because database is full */#define SQLITE_CANTOPEN    14   /* Unable to open the database file */#define SQLITE_PROTOCOL    15   /* Database lock protocol error */#define SQLITE_EMPTY       16   /* Database is empty */#define SQLITE_SCHEMA      17   /* The database schema changed */#define SQLITE_TOOBIG      18   /* String or BLOB exceeds size limit */#define SQLITE_CONSTRAINT  19   /* Abort due to constraint violation */#define SQLITE_MISMATCH    20   /* Data type mismatch */#define SQLITE_MISUSE      21   /* Library used incorrectly */#define SQLITE_NOLFS       22   /* Uses OS features not supported on host */#define SQLITE_AUTH        23   /* Authorization denied */#define SQLITE_FORMAT      24   /* Auxiliary database format error */#define SQLITE_RANGE       25   /* 2nd parameter to sqlite3_bind out of range */#define SQLITE_NOTADB      26   /* File opened that is not a database file */#define SQLITE_ROW         100  /* sqlite3_step() has another row ready */#define SQLITE_DONE        101  /* sqlite3_step() has finished executing *//* end-of-error-codes */

With this, we can debug our code more conveniently.

Now, let's go to the topic. The above function is used to open the database when the database exists. If the database does not exist, the name of the database can be randomly fetched, as long as it is an ASCII character, because the SQLite database is originally an ASCII file (so its security is not big, but there is no problem as a local database ).

After the database is opened, we can perform a series of addition, deletion, query, and modification operations. After the operation, we need to close the database, right? Otherwise, how can we release resources?

Closing is also simple:

SQLITE_API int sqlite3_close(sqlite3 *db);

You don't need to explain this. view my instance directly:

Sqlite3_close (PDB); // you can ignore the returned value here.

Okay, this section is over again. Is it easy. OK. Let's look forward to the next section. We will learn how to create a table in the next section.

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.