INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)]
VALUES (value1, value2, value3,...valueN);
在这里,column1, column2,…columnN 是要插入数据的表中的列的名称。 如果要为表中的所有列添加值,您也可以不需要在 SQLite 查询中指定列名称。但要确保值的顺序与列在表中的顺序一致。SQLite 的 INSERT INTO 语法如下:
INSERT INTO TABLE_NAME VALUES (value1,value2,value3,...valueN);
实例
假设您已经在 testDB.db 中创建了 COMPANY 表,如下所示:
sqlite> CREATE TABLE COMPANY(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL
);
现在,下面的语句将在 COMPANY 表中创建六个记录:
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Paul', 32, 'California', 20000.00 );
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (2, 'Allen', 25, 'Texas', 15000.00 );
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (5, 'David', 27, 'Texas', 85000.00 );
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (6, 'Kim', 22, 'South-Hall', 45000.00 );
您也可以使用第二种语法在 COMPANY 表中创建一个记录,如下所示:
INSERT INTO COMPANY VALUES (7, 'James', 24, 'Houston', 10000.00 );
上面的所有语句将在 COMPANY 表中创建下列记录。下一章会教您如何从一个表中显示所有这些记录。
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
1 Paul 32 California 20000.0
2 Allen 25 Texas 15000.0
3 Teddy 23 Norway 20000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
6 Kim 22 South-Hall 45000.0
7 James 24 Houston 10000.0
使用一个表来填充另一个表
您可以通过在一个有一组字段的表上使用 select 语句,填充数据到另一个表中。下面是语法:
INSERT INTO first_table_name [(column1, column2, ... columnN)]
SELECT column1, column2, ...columnN
FROM second_table_name
[WHERE condition];
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
1 Paul 32 California 20000.0
2 Allen 25 Texas 15000.0
3 Teddy 23 Norway 20000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
6 Kim 22 South-Hall 45000.0
7 James 24 Houston 10000.0
sqlite>.header on
sqlite>.mode column
sqlite> SELECT * FROM COMPANY;
最后,将得到以下的结果:
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
1 Paul 32 California 20000.0
2 Allen 25 Texas 15000.0
3 Teddy 23 Norway 20000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
6 Kim 22 South-Hall 45000.0
7 James 24 Houston 10000.0
如果只想获取 COMPANY 表中指定的字段,则使用下面的查询:
sqlite> SELECT ID, NAME, SALARY FROM COMPANY;
上面的查询会产生以下结果:
ID NAME SALARY
---------- ---------- ----------
1 Paul 20000.0
2 Allen 15000.0
3 Teddy 20000.0
4 Mark 65000.0
5 David 85000.0
6 Kim 45000.0
7 James 10000.0
ID NAME AGE ADDRESS SALARY
---------- -------------------- ---------- ---------- ----------
1 Paul 32 California 20000.0
2 Allen 25 Texas 15000.0
3 Teddy 23 Norway 20000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
6 Kim 22 South-Hall 45000.0
7 James 24 Houston 10000.0
sqlite>.databases
seq name file
---- ---------------- --------------------------------------------
0 main /Users/wangruofeng/Documents/SQLitePractice/testDB.db
sqlite> .database
seq name file
--- --------------- ----------------------
0 main /home/sqlite/testDB.db
2 test /home/sqlite/testDB.db
数据库名称 main 和 temp 被保留用于主数据库和存储临时表及其他临时数据对象的数据库。这两个数据库名称可用于每个数据库连接,且不应该被用于附加,否则将得到一个警告消息,如下所示:
sqlite> ATTACH DATABASE 'testDB.db' as 'TEMP';
Error: database TEMP is already in use
sqlite> ATTACH DATABASE 'testDB.db' as 'main';
Error: database main is already in use
ERROR :Could not find a valid gem `cocoapods` (>= 0), here is why: Unable to download data from https://rubygems.org/ - Errno::EPIPI: Broken pipe - SSL_connect (https://rubygems.org/lastest_specs.4.8.gz)
安装成功后,接着执行命令:
1
pod setup
如果 Ruby 环境不够新,可能需要更新以下:
1
sudo gem update --system
至此安装就完成了,我们可以尝试搜索一个第三方类库:
1
pod search AFNetworking
使用
使用 CocoaPods 第一步,是在当前项目下,新建一个 Podfile 文件:
1
touch Podfile
然后利用 vim 打开 Podfile 文件编辑,加入你想要的类库,格式如下:
1 2 3 4 5 6
platform :ios pod 'Reachability', '3.1.0'
platform :ios, '6.0' pod 'JSONKit', '1.4' pod 'AFNetworking', '~> 2.3.1'
I’m sending over three variables: an image named photo, a string named message, and an integer named user. It’s important to note the linebreaks and the dashes before the boundary string. These must be included in order to build a good request. Now lets write some objective-c:
“Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.”
Post info: Updated for Xcode 7.1 and Swift 2.1 – 1 January 2016 Update note: This tutorial was updated to iOS 8, Xcode 6.1 and Swift by Richard Turton. Original post by Tutorial Team member Soheil Azarpour.
每个人都有点击一个按钮或者进入一些文本在 iOS 或者 Mac App 上的令人沮丧的经历,就是突然-WHAM,用户交互停止了响应。
funcfetchPhotoDetails() { let request =NSURLRequest(URL:dataSourceURL!) UIApplication.sharedApplication().networkActivityIndicatorVisible =true
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {response,data,error in if data !=nil { let datasourceDictionary =NSPropertyListSerialization.propertyListWithData(data, options: Int(NSPropertyListMutabilityOptions.Immutable.rawValue), format: nil, error: nil) as!NSDictionary
for(key : AnyObject,value : AnyObject) in datasourceDictionary { let name = key as?String let url =NSURL(string:value as?String??"") if name !=nil&& url !=nil { let photoRecord =PhotoRecord(name:name!, url:url!) self.photos.append(photoRecord) } }
self.tableView.reloadData() }
if error !=nil { let alert =UIAlertView(title:"Oops!",message:error.localizedDescription, delegate:nil, cancelButtonTitle:"OK") alert.show() } UIApplication.sharedApplication().networkActivityIndicatorVisible =false } }
//4 switch (photoDetails.state){ case .Filtered: indicator.stopAnimating() case .Failed: indicator.stopAnimating() cell.textLabel?.text ="Failed to load" case .New, .Downloaded: indicator.startAnimating() self.startOperationsForPhotoRecord(photoDetails,indexPath:indexPath) }
注意:此教程写于 Update 17 April 2015: Updated for Xcode 6.3 and Swift 1.2 ,现在 Swift 最新版本 2.1 使用 Xcode7+以上编辑会报错,这里打包一个新语法修改版completed fixed version of the project。
// MyDownloadOperation is a subclass of NSOperation let downloadOperation =MyDownloadOperation() // MyFilterOperation is a subclass of NSOperation let filterOperation =MyFilterOperation()