我正在建立一个通过各种CSV Feed更新大量数据的系统.通常我会循环遍历Feed中的每一行,执行select查询以检查项目是否已存在,并根据项目是否存在插入/更新项目.
我觉得这种方法的可扩展性不高,可能会使服务器在更大的源上运行.我的解决方案是正常循环项目,但将它们存储在内存中.然后,对于每100个左右的项目,对这100个项目进行选择,并获得数据库中匹配的现有项目列表.然后将insert/update语句连接在一起并将它们运行到数据库中.这基本上可以减少到数据库的行程.
这是一个可扩展的解决方案吗?是否有关于将大型Feed导入生产环境的示例教程?
谢谢
我写了下面的代码,但它似乎只插入当前日期而不是当前时间.谁知道怎么做?
insert into errortable
(dateupdated,table1id)
values
(TO_DATE(sysdate, 'dd/mm/yyyy hh24:mi:ss'),1083);
Run Code Online (Sandbox Code Playgroud) 我有以下表结构:
CREATE TABLE IF NOT EXISTS `reports` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`day` int(11) NOT NULL,
`uid` int(11) NOT NULL,
`siteid` int(11) NOT NULL,
`cid` int(3) NOT NULL,
`visits` int(11) NOT NULL,
PRIMARY KEY (`id`)
)
Run Code Online (Sandbox Code Playgroud)
目前我使用以下代码片段检查并插入/更新:
$checkq = mysql_query("SELECT count(*) as rowexist FROM reports WHERE day='$day' AND uid='$uid' AND siteid='$sid' AND cid='$cid'") or die(mysql_error());
$checkr = mysql_fetch_array($checkq);
if ($checkr['rowexist'] > 0) {
mysql_query("UPDATE reports_adv SET visits=visits+1 WHERE day='$day' AND uid='$uid' AND siteid='$sid' AND cid='$cid'");
} else {
mysql_query("INSERT …Run Code Online (Sandbox Code Playgroud) 我一直在看这段代码一段时间,我看不出问题出在哪里.我一直在阅读整个StackOverflow,但仍无法看到我的错误.
<?php
mysqli_connect("localhost","root","","web_table");
mysql_select_db("web_table") or die(mysql_error());
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo "<p> Connection Successful!"
mysqli_query('INSERT INTO web_formitem (ID, formID, caption, key, sortorder, type, enabled, mandatory, data) VALUES (105, 7, Tip izdelka (6), producttype_6, 42, 5, 1, 0, 0)');
echo "<p>Insert successfull";
?>
Run Code Online (Sandbox Code Playgroud)
错误在第13行的某处,那就是mysqli_query('insert....我试图用http://www.w3schools.com/php/php_mysql_insert.asp来帮助自己,但这对我帮助不大.
我试图将None值插入我的数据库的行条目.该表present存在
db.execute("INSERT INTO present VALUES('test', ?, 9)", "This is a test!")
db.execute("INSERT INTO present VALUES('test2', ?, 10)", None)
Run Code Online (Sandbox Code Playgroud)
但是我收到一个错误:
ValueError: parameters are of unsupported type
Run Code Online (Sandbox Code Playgroud)
如何为行中的第二个字段插入空白值?
我有一个问题,并希望有人可以帮助我.
我的问题是每当我在"MainFile"中运行代码时,它会输出它添加了条目的两个ID,但是当我查看我的数据库时,我有六个条目而不是两个...有人可以告诉我为什么它可能会这样做?
注意:我只提供了类中代码的片段,因为提供完整的类将是太多的代码.这是唯一可以执行的代码.
编辑史上的代码
编辑:我已经在这篇文章的底部添加了一个调试日志,验证我只为每个查询执行一次SQL代码.
编辑:我不再使用序列化方法,因为存储此类数据显然不是一个好主意.但是,即使我的新代码将每个单独的值存储在数据库中,我仍然会得到三个条目而不是一个条目.所以,这是同一个问题.什么?
编辑:经过几天的调试后,我已将其缩小到导致问题的这一行
Throwlite::$systemSQL->executeSql("INSERT into ".SQL_COMMENTTHREADS_TABLE." (id, sort_order) values (DEFAULT, '2')");
Run Code Online (Sandbox Code Playgroud)
您可以在此处查看LiteSQL类以供参考:http://pastebin.com/a4C6fF4u
另外,作为参考,以下是用于创建表的代码:
"CREATE TABLE IF NOT EXISTS `" . SQL_COMMENTTHREADS_TABLE . "` (`id` int unsigned NOT NULL AUTO_INCREMENT, `sort_order` int NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;"
Run Code Online (Sandbox Code Playgroud)
而且,即使我非常确定它应该没关系,这里是定义SQL_COMMENTTHREADS_TABLE的地方.
define( 'SQL_COMMENTTHREADS_TABLE', "tl_comment_threads");
Run Code Online (Sandbox Code Playgroud) 如果标签和 url 组合已存在,我会尝试忽略插入:
INSERT INTO tags(tag, url) VALUES (?, ?);
ON CONFLICT(url, tag) IGNORE
Run Code Online (Sandbox Code Playgroud)
我有一个唯一索引(tag, url)
CREATE UNIQUE INDEX tag_url ON tags (tag, url)
Run Code Online (Sandbox Code Playgroud)
该应用程序只是崩溃说[uncaught application error]: SqliteError - UNIQUE constraint failed: tags.tag, tags.url
可能重复:
在Oracle中进行多行插入的最佳方法是什么?
我有这个插入语句
INSERT INTO mytable VALUES
('val1', 'val2'),
('aa', 'cc'),
('ww', 'dd');
Run Code Online (Sandbox Code Playgroud)
我收到此错误ORA-00933:SQL命令未正确结束
我该如何插入array of enums?
这是我的enum:
CREATE TYPE equipment AS ENUM ('projector','PAsystem','safe','PC','phone');
Run Code Online (Sandbox Code Playgroud)
然后我的桌子上有一系列设备:
CREATE TABLE lecture_room (
id INTEGER DEFAULT NEXTVAL('lecture_id_seq')
, seatCount int
, equipment equipment[]
) INHERITS(venue);
Run Code Online (Sandbox Code Playgroud)
这是我要插入的ATTEMPT:
INSERT INTO lecture_room (building_code, floorNo, roomNo, length, width
, seatCount, equipment)
VALUES
('IT', 4, 2, 10, 15 ,120, ARRAY['projector','PAsystem','safe']),
Run Code Online (Sandbox Code Playgroud)
但它给了我以下错误:
Run Code Online (Sandbox Code Playgroud)ERROR: column "equipment" is of type equipment[] but expression is of type text[] SQL state: 42804 Hint: You will need to rewrite or cast the expression.
我有stock_price_alert3列的表.stock_price_id是PRIMARY KEY和FOREIGN KEY其他表.表定义如下:
create table stock_price_alert (
stock_price_id integer references stock_price (id) on delete cascade not null,
fall_below_alert boolean not null,
rise_above_alert boolean not null,
primary key (stock_price_id)
);
Run Code Online (Sandbox Code Playgroud)
我需要:
1)INSERT记录如果不存在
-- query 1
INSERT INTO stock_price_alert (stock_price_id, fall_below_alert, rise_above_alert)
VALUES (1, true, false);
Run Code Online (Sandbox Code Playgroud)
2)UPDATE记录是否存在
-- query 2
UPDATE stock_price_alert SET
fall_below_alert = true,
rise_above_alert = false
WHERE stock_price_id = 1;
Run Code Online (Sandbox Code Playgroud)
首先,我需要SELECT在stock_price_alert表上发出查询,以决定是执行查询(1)还是(2).
Postgres支持INSERT …
sql-insert ×10
sql ×5
php ×3
sql-update ×3
mysql ×2
postgresql ×2
sqlite ×2
csv ×1
database ×1
datetime ×1
duplicates ×1
enums ×1
feeds ×1
mysqli ×1
oracle ×1
oracle10g ×1
python ×1
types ×1
upsert ×1