SQLite creates a table and adds data. sqlite creates data.

Source: Internet
Author: User

SQLite creates a table and adds data. sqlite creates data.

-(Void) viewDidLoad {[super viewDidLoad]; // create a table [self creatTable]; // insert data [self insertTable];} // --------------------- create a table ------------------- (void) creatTable {// 1. create a database object sqlite3 * sqlite3 = nil; // 2. path of the database NSString * path = [NSHomeDirectory () stringByAppendingPathComponent: @ "Documents/mySqlite. db "]; // 3. open the database (open the database file through the specified path, if not, create it) int result = sqlite3_open ([path UTF8String], & sqlite3 ); If (result! = SQLITE_ OK) {NSLog (@ "failed to open the database! "); Return;} // 4. CREATE an SQL statement NSString * SQL = @ "CREATE TABLE Students (id integer PRIMARY KEY, name text)"; // 5. run the SQL statement char * error = NULL; result = sqlite3_exec (sqlite3, [SQL UTF8String], NULL, NULL, & error); if (result! = SQLITE_ OK) {NSLog (@ "An error occurred while executing the SQL statement! "); // 6. close the database sqlite3_close (sqlite3); return;} // 6. close the database sqlite3_close (sqlite3);} // --------------------------- insert data ------------------------- (void) insertTable {// 1. create a database object sqlite3 * sqlite3 = nil; // 2. path of the database NSString * path = [NSHomeDirectory () stringByAppendingPathComponent: @ "Documents/mySqlite. db "]; // 3. open the database (open the database file through the specified path, if not, create it) int result = sqlite3_open ([path UTF8String], & sqlite3); if (re Sult! = SQLITE_ OK) {NSLog (@ "failed to open the database! "); Return;} // 4. create an SQL statement // insert into students (id, name) values ('000000', 'riss') NSString * SQL = @ "insert into students (id, name) values (?,?) "; // 5. compile an SQL statement // create a data handle object sqlite3_stmt * stmt = nil; result = sqlite3_prepare_v2 (sqlite3, [SQL UTF8String],-1, & stmt, nil); if (result! = SQLITE_ OK) {NSLog (@ "Compilation failed"); // close the database sqlite3_close (sqlite3); return ;}// 6. bind data to sqlite3_bind_int (stmt, 1, 123457) in the Data handle; sqlite3_bind_text (stmt, 2, "zhangsan",-1, nil); // 7. result = sqlite3_step (stmt); if (result = SQLITE_ERROR | result = SQLITE_MISUSE) {NSLog (@ "insertion failed "); // close the data handle sqlite3_finalize (stmt); // close the database sqlite3_close (sqlite3); return ;}// 8. NSLog (@ "inserted successfully"); // close the data handle sqlite3_finalize (stmt); // close the database sqlite3_close (sqlite3 );}


In android, how does one add columns or update versions to a table after creating a database or table?

// Define the UPDATE function
Private void upgradeDatabaseToVersion1 (SQLiteDatabase db ){
// Add 'new' column to mytable table.
Db.exe cSQL ("alter table mytable add column new TEXT ");
}

// Rewrite onUpgrade
Public void onUpgrade (SQLiteDatabase db, int oldVersion, int currentVersion ){
Log. w (TAG, "Upgrading database from version" + oldVersion
+ "To" + currentVersion + ".");

Switch (oldVersion ){
Case 0:
If (currentVersion <= 1 ){
Return;
}

Db. beginTransaction ();
Try {
UpgradeDatabaseToVersion1 (db );
Db. setTransactionSuccessful ();
} Catch (Throwable ex ){
Log. e (TAG, ex. getMessage (), ex );
Break;
} Finally {
Db. endTransaction ();
}

Return;
}

Log. e (TAG, "Destroying all old data .");
DropAll (db );
OnCreate (db );
}

Bytes -----------------------------------------------------------------------------------------------------
Answers from the android team
Welcome to the android excellent team

In android, how does one add columns to a table after creating a database and a table?

Alter table download_info ADD state Integer
 

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.