SQLite插入值并选择

时间:2015-07-02 08:03:28

标签: sqlite

我需要插入如下行:

INSERT INTO table1 (id, name) VALUES (1, SELECT otherName FROM table2);

在SQLite中可以吗?

4 个答案:

答案 0 :(得分:1)

以下是您要查找的语法:

INSERT INTO table1 (id, name)
SELECT 1, otherName
FROM table2

查看涵盖类似问题的this SO article

答案 1 :(得分:1)

来自这里:http://www.tutorialspoint.com/sqlite/sqlite_insert_query.htm

  

您可以通过select语句将数据填充到表中   另一个表提供了另一个表有一组字段,它们是   需要填充第一个表。这是语法:

+ (void) getJsonDataFromURL:(NSString *)urlString
            completionBlock:(void(^)(NSDictionary* response))completion {
       NSURLSession *session = [NSURLSession sharedSession];
       NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:urlString] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
           NSDictionary* jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
           NSLog(@"%@", jsonResponse);

           completion(jsonResponse);
       }];

       [dataTask resume];
}

答案 2 :(得分:0)

这有效:

INSERT INTO test2 values(1,(Select name from test1 where id=1))

我在SQLite上测试了它:http://sqlfiddle.com/#!5/9bcff/1

答案 3 :(得分:-1)

AM使用java,所以你可以使用像这样的东西

prep = Db_Connector.connection.prepareStatement("insert into reg_member values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
            prep.setString(2, surname);
            prep.setString(3, lastname);         
            prep.execute();
            prep.close();
            Db_Connector.killObjects(Db_Connector.statement, Db_Connector.connection);
相关问题