English 中文(简体)
甲型六氯环己烷
原标题:Python - Bulk Select then Insert from one DB to another

I m 寻求某种帮助,了解如何利用Sqlite3在沙尔这样做。

基本上,我有一个过程,下载一个行文,然后需要将其记录列入第二个完全相同的行文(主编),同时忽视/绕过任何可能的重复重大错误。

我正在考虑两种情景,但对于如何最好地在沙尔这样做,我感到不舒服。

备选案文1:

  • create 2 connections and cursor objects, 1 to each DB
  • 选自第1行:

    dbcur.executemany( SELECT * from table1 )
    rows = dbcur.fetchall()
    
  • 插入第2行:

    dbcur.execute( INSERT INTO table1 VALUES (:column1, :column2) , rows)
    dbcon.commit()
    

当然,这并不奏效,因为我不知道如何适当做到这一点:

Option 2 (which I would prefer, but not sure how to do):

  • SELECT and INSERT in 1 statement

此外,我有4个表格,各栏各有不同的栏目,我是否能够熟练地命名INSERT发言栏目?

As far as the duplicate keys go, I have read I can use ON DUPLICATE KEY to handle eg.

INSERT INTO table1 VALUES (:column1, :column2) ON DUPLICATE KEY UPDATE set column1=column1
最佳回答

http://www.sqlite.org/lang_attach.html”rel=“nofollow noreferer”

import sqlite3
connection = sqlite3.connect( /path/to/temp.sqlite )
cursor=connection.cursor()
cursor.execute( ATTACH "/path/to/main.sqlite" AS master )

在MySQL.上,没有发现DUPLICATE KEY syntax。 本SO问题包含替代物。

So to do the bulk insert in one sql statement, you could use something like

cursor.execute( INSERT OR REPLACE INTO master.table1 SELECT * FROM table1 )

See this page for information about REPLACE and other ON CONFLICT options.

问题回答

备选案文1的守则是正确的。

如果你需要过滤,以绕过钥匙,则在临时表格中插入,然后使用KQ,消除重复,将其并入目标表。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签