我做以下
INSERT INTO 'tablename'
SELECT 'data1' AS 'column1', 'data2' AS 'column2'
UNION SELECT 'data3', 'data4'
UNION SELECT 'data5', 'data6'
UNION SELECT 'data7', 'data8'
来自LINK!
查询实际执行没有问题,但没有插入数据......我在最后做了简单的测试......
这是插入LOG:
05-18 10:53:49.531: DEBUG/dalvikvm(27793): GC_CONCURRENT freed 1001K, 52% free 3602K/7367K, external 2526K/3051K, paused 1ms+2ms
05-18 10:53:49.785: DEBUG/dalvikvm(27793): GC_CONCURRENT freed 1117K, 51% free 3610K/7367K, external 2526K/3051K, paused 8ms+2ms
05-18 10:53:49.835: DEBUG/dalvikvm(27793): GC_CONCURRENT freed 1083K, 52% free 3599K/7367K, external 2526K/3051K, paused 2ms+2ms
05-18 10:53:49.890: DEBUG/dalvikvm(27793): GC_CONCURRENT freed 1043K, 51% free 3634K/7367K, external 2526K/3051K, paused 2ms+2ms
05-18 10:53:49.917: DEBUG/dalvikvm(27793): GC_FOR_MALLOC freed 1079K, 52% free 3578K/7367K, external 2526K/3051K, paused 18ms
05-18 10:53:49.964: DEBUG/dalvikvm(27793): GC_CONCURRENT freed 987K, 51% free 3657K/7367K, external 2526K/3051K, paused 2ms+1ms
05-18 10:53:49.988: DEBUG/dalvikvm(27793): GC_FOR_MALLOC freed 955K, 52% free 3606K/7367K, external 2526K/3051K, paused 17ms
05-18 10:53:50.011: DEBUG/dalvikvm(27793): GC_FOR_MALLOC freed 1037K, 52% free 3584K/7367K, external 2526K/3051K, paused 18ms
05-18 10:53:50.035: DEBUG/dalvikvm(27793): GC_FOR_MALLOC freed 953K, 51% free 3627K/7367K, external 2526K/3051K, paused 18ms
05-18 10:53:50.058: DEBUG/dalvikvm(27793): GC_FOR_MALLOC freed 925K, 51% free 3631K/7367K, external 2526K/3051K, paused 18ms
05-18 10:53:50.089: DEBUG/dalvikvm(27793): GC_FOR_MALLOC freed 945K, 52% free 3589K/7367K, external 2526K/3051K, paused 18ms
05-18 10:53:50.113: DEBUG/dalvikvm(27793): GC_FOR_MALLOC freed 883K, 51% free 3638K/7367K, external 2526K/3051K, paused 18ms
05-18 10:53:50.132: DEBUG/dalvikvm(27793): GC_FOR_MALLOC freed 857K, 51% free 3625K/7367K, external 2526K/3051K, paused 17ms
05-18 10:53:50.156: DEBUG/dalvikvm(27793): GC_FOR_MALLOC freed 924K, 52% free 3593K/7367K, external 2526K/3051K, paused 18ms
05-18 10:53:50.183: DEBUG/dalvikvm(27793): GC_FOR_MALLOC freed 925K, 52% free 3594K/7367K, external 2526K/3051K, paused 19ms
05-18 10:53:50.203: DEBUG/dalvikvm(27793): GC_FOR_MALLOC freed 959K, 52% free 3596K/7367K, external 2526K/3051K, paused 18ms
05-18 10:53:50.226: DEBUG/dalvikvm(27793): GC_FOR_MALLOC freed 859K, 52% free 3597K/7367K, external 2526K/3051K, paused 18ms
05-18 10:53:50.226: DEBUG/TV Spored++(27793): INSERT INTO current SELECT '1' AS _id_current, '4645416' AS id_current, '3SAT' AS channel_name, '3SAT' AS channel, '2' AS id_channel, '27' AS id_language, 'Lanz kuha' AS title, '2011-05-18 10:15:00' AS start, '75' AS length, '11' AS catergory, '65' AS genre, '0' AS sortOrder UNION SELECT '2', '4628869', 'A Kanal', 'AKANAL', '3', '2', 'Pa me ustreli!', '2011-05-18 10:40:00', '30', '5', '94', '1' UNION SELECT '3', '4648393', 'ABMoteurs', 'ABMOTEURS', '4', '12', 'Fenomen Ferrari', '2011-05-18 10:30:00', '30', '8', '7', '2' UNION SELECT '4', '4644514', 'ARD', 'ARD', '8', '27', 'Za božjo voljo', '2011-05-18 10:25:00', '50', '5', '9', '3' UNION SELECT '5', '4647462', 'ARTE', 'ARTE', '9', '27', 'Junaki znanosti', '2011-05-18 10:10:00', '50', '8', '7', '4' UNION SELECT '6', '4624746', 'AXN Adria', 'AXN', '164', '2', 'Starsky in Hutch', '2011-05-18 10:40:00', '60', '5', '71', '5' UNION SELECT '7', '4632973', 'Animal Planet', 'ANIMAL', '6', '2', 'Živalsko okrožje', '2011-05-18 10:00:00', '55', '8', '103', '6' UNION SELECT '8', '4646744', 'BBC Entertainment', 'BBCPRIME', '10', '2', 'Michael Palin v Novi Evropi', '2011-05-18 10:25:00', '50', '8', '103', '7' UNION SELECT '9', '4645887', 'BBC World', 'BBCWORLD', '11', '2', 'Težke besede', '2011-05-18 10:30:00', '30', '7', '39', '8' UNION SELECT '10', '4640146', 'Baby First', 'BABYFIRST', '134', '2', 'Različno', '2011-05-18 10:30:00', '60', '6', '0', '9' UNION SELECT '11', '4638896', 'Boomerang', 'BOOMERANG', '12', '2', 'Jetsonovi', '2011-05-18 10:35:00', '25', '6', '46', '10' UNION SELECT '12', '4634922', 'C Music TV', 'CMUSICTV', '124', '2', 'Videomix', '2011-05-18 10:00:00', '240', '11', '15', '11' UNION SELECT '13', '4573091', 'CCTV Kitajski', 'CCTVCHINA', '149', '2', 'Potovanja', '2011-05-18 10:30:00', '30', '8', '103', '12' UNION SELECT '14', '4659684', 'CNBC Europe', 'CNBCEUR', '150', '2', 'Mednarodno trgovanje', '2011-05-18 10:00:00', '120', '7', '20', '13' UNION SELECT '15', '4640257', 'CNN International', 'CNN', '16', '2', 'Poslovni svet danes', '2011-05-18 10:00:00', '60', '7', '20', '14' UNION SELECT '16', '4638021', 'Cartoon Network - TCM', 'CARTOON', '14', '2', 'Robotek', '2011-05-18 10:30:00', '30', '6', '46', '15' UNION SELECT '17', '4646081', 'Channel One Russia', 'RUSSIA1', '165', '34', 'Zdravo živeti!', '2011-05-18 09:50:00', '70', '9', '103', '16' UNION SELECT '18', '4644397', 'Cinemax', 'CINEMAX1', '111', '2', 'Mamma Mia!', '2011-05-18 09:40:00', '110', '1', '97', '17' UNION SELECT '19', '4643819', 'Cinemax 2', 'CINEMAX2', '113', '2', 'Kalifornijska delitev', '2011-05-18 10:00:00', '105', '1', '24', '18' UNION SELECT '20', '4615025', 'Croatian Music Channel', 'CMC', '17', '14', 'Nova plošča', '2011-05-18 10:00:00', '60', '11', '15', '19' UNION SELECT '21', '4604102', 'DM Sat', 'DMSAT', '151', '38', 'Glasbeni program in pogovor preko SMS-ov', '2011-05-18 07:05:00', '235', '11', '15', '20' UNION SELECT '22', '4645977', 'Da Vinci Learning', 'DAVINCILEAR', '121', '2', 'Nekoč so bili izumitelji: Einstein', '2011-05-18 10:30:00', '30', '6', '22', '21' UNION SELECT '23', '4649979', 'Daring! TV', 'XXXXTREME', '159', '2', 'Fantom vstop prepovedan', '2011-05-18 08:53:00', '127', '14', '91', '22' UNION SELECT '24', '4626679', 'Discovery Channel Europe', 'DISCOVERY', '19', '2', 'Neverjetno preživetje', '2011-05-18 10:00:00', '55', '8', '103', '23' UNION SELECT '25', '4613894', 'Discovery HD', 'DISCOVERYHD', '131', '3', 'GT dirkalnik: Magny Cours', '2011-05-18 10:35:00', '50', '8', '52', '24' UNION SELECT '26', '4633183', 'Discovery Investigation', 'DISCINV', '120', '2', 'Dosjeji FBI', '2011-05-18 10:10:00', '50', '8', '103', '25' UNION SELECT '27', '4610298', 'Discovery Science', 'DISCSCI', '21', '2', 'LDRS - Največji nevarni raketoplani', '2011-05-18 10:50:00', '50', '8', '103', '26'
这里有什么问题?
感谢您的回答!
答案 0 :(得分:0)
尝试启动一个事务,因此所有这些插入都可以组合在一起。除非您明确地启动和结束事务,否则AFAIK SQLite会将每个插入视为具有单个事务。使用Begin和Commit。
答案 1 :(得分:0)
在您的LOG中GC_FOR_MALLOC
表示您的垃圾收集器必须经常执行,可能是由于查询的大小。如果必须多次连接字符串,请避免使用“+”运算符,因为它会为每个连接创建一个新的字符串。因此,垃圾收集器必须工作很多才能收集这些垃圾strings
而不是stringBuilder
。字符串构建器将新字符串附加到先前,并且不会每次都创建新字符串。
StringBuilder s0 = "INSERT INTO ".append( DATABASE_TABLE_CURRENT )
.append( " (" ).append( KEY_ID_CURRENT ).append( ", ").append( KEY_CURRENT_CHANNEL_NAME )
.append( ", " ).append( KEY_CURRENT_CHANNEL )
.append( ", " ).append( KEY_CURRENT_ID_CHANNEL ).append( ", " ).append( KEY_CURRENT_ID_LANG )
.append( ", " ).append( KEY_CURRENT_TITLE ).append( ", " ).append( KEY_CURRENT_START )
.append( ", " ).append( KEY_CURRENT_LENGTH ).append( ", " ).append( KEY_CURRENT_CATEGORY )
.append( ", " ).append( KEY_CURRENT_GENRE ).append( ", " ).append( KEY_CURRENT_ORDER ).append( ") ")
.append( "SELECT '4628870', 'A Kanal', 'AKANAL', '3', '2', 'Obalna straža', " ).append(
"'2011-05-18 11:10:00', '55', '5', '9', '1'; ");
Log.d("TV Spored++", s0.toString());
db.rawQuery(s0.toString(), null);
至于考虑查询的语法,查询结构完美。
INSERT INTO 'tablename' ('column1', 'column2')
SELECT 'data3', 'data4'
UNION SELECT 'data5', 'data6'
UNION SELECT 'data7', 'data8'
我建议您使用execSQL
代替rawQuery(string,null)
。由于execSQL
用于查询并不意味着返回任何内容。
db.execSQL(s0.toString());