Use Sqlite3 command line under MAC

Source: Internet
Author: User
Tags sqlite

Sqlite3 command line simple to use

Sqlite3 Path//Open Database path connection
SELECT * from Sqlite_master where type= "table"; Show All Tables
SELECT * from testable; Display data for a table
. Help//View assistance
. Quit//exit

Xcode use Sqlite3 Step 1. Add Libsqlite3.dylib

2. Header files
////DbUtils.h//Smart////Created by Xie Han Festival on 15/5/12.//Copyright (c) 2015 WHR. All rights reserved.//#import <Foundation/Foundation.h> #import "sqlite3.h"  @interface dbutils : nsobject {Sqlite3 *db;//Declare a sqlite3 database}- (NSString*) FilePath;the path to the//database file. Generally in the sandbox documents inside the Operation-(void) opendb;-(void) closedb;-(Nsmutablearray*) Getalltypes;@end

. m file

////DBUTILS.M//Smart////Created by Xie Han Festival on 15/5/12.//Copyright (c) 2015 WHR. All rights reserved.//#import "DbUtils.h" #import "KMTypes.h" #import "KMContents.h"  @implementation dbutils //How to open a database- (void) opendb{whether or not the file exists    Nsfilemanager* FileManager = [NsfilemanagerDefaultmanager];NSString*dbpath=[ SelfFilePath];NSLog(@"Database path:%@", DBPath);BOOLSuccess = [FileManager Fileexistsatpath:dbpath];if(!success) {NSString*resourcepath=[[NSBundleMainbundle]resourcepath];//Automatic replication        NSString*sourcedbpath=[resourcepath stringbyappendingpathcomponent:@"App.bundle/datas.sqlite"];Nserror*error; Success = [FileManager Copyitematpath:sourcedbpath topath:dbpath error:&error];if(!success) NSAssert1 (0,@"Database attach failed!" '%@ '. ", [error localizeddescription]);Else            NSLog(@"Database Attach succeeded:%@", DBPath); }if(Sqlite3_open ([[ SelfFilePath] utf8string], &db)! = SQLITE_OK) {sqlite3_close (db); Nsassert (0, @"Database open failed. "); }}- (void) closedb{sqlite3_close (db);}//This method is used to return the full path information of the database in the Documents folder- (NSString*) filepath{Nsarray*paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask,YES);NSString*documentsdir = [Paths Objectatindex:0];return[Documentsdir stringbyappendingpathcomponent:@"Datas.sqlite"];}////Querying data all categories- (Nsmutablearray*) getalltypes{[ SelfOPENDB];Nsmutablearray*array=[NsmutablearrayArraywithcapacity:6];NSString*sql = @"SELECT * from Km_types"; Sqlite3_stmt *statement;if(SQLITE3_PREPARE_V2 (DB, [SQL Utf8string],-1, &statement,Nil) = = SQLITE_OK) { while(Sqlite3_step (statement) = = Sqlite_row) {kmtypes* k= [[Kmtypes alloc]init];inttype_id = (int) Sqlite3_column_int (statement,0);intparent_id = (int) Sqlite3_column_int (statement,1);Char*type_title = (Char*) Sqlite3_column_text (statement,2);intType_order = (int) Sqlite3_column_int (statement,3);intTopic_count= (int) Sqlite3_column_int (statement,4);NSString*TYPE_TITLESTR = [[NSStringAlloc] Initwithutf8string:type_title]; K. Type_title= Type_titlestr; K. type_id=type_id; K. parent_id= parent_id; K. Type_order= Type_order; K. Topic_count=topic_count;        [Array addobject:k];    } sqlite3_finalize (statement); }    [ SelfClosedb];returnArray;}@endOnly one simple query function is implemented here.

Use Sqlite3 command line under MAC

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.