Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #42165

Re: Replacing old data with new data using python

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.034
X-Spam-Evidence '*H*': 0.93; '*S*': 0.00; 'insert': 0.05; 'much!': 0.05; 'string': 0.09; 'assuming': 0.09; 'clean.': 0.09; 'committing': 0.09; 'statements': 0.09; 'subject:using': 0.09; '"insert': 0.16; '%d,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'given,': 0.16; 'happily': 0.16; 'preserve': 0.16; 'semantics': 0.16; 'statement.': 0.16; 'version;': 0.16; 'subject:python': 0.16; 'do,': 0.16; 'wrote:': 0.18; 'do.': 0.18; 'select': 0.22; "i've": 0.25; 'this:': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'field,': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'accidentally': 0.31; 'table,': 0.31; 'fri,': 0.33; 'maybe': 0.34; 'skip:d 20': 0.34; 'subject:with': 0.35; 'received:209.85': 0.35; 'received:209.85.220': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'subject:data': 0.36; 'doing': 0.36; 'too': 0.37; 'received:209': 0.37; 'subject:new': 0.38; 'to:addr:python-list': 0.38; 'previous': 0.38; 'rather': 0.38; 'structure': 0.39; 'delete': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'remove': 0.60; 'course.': 0.60; 'transaction.': 0.60; 'reserved': 0.61; 'new': 0.61; 'simply': 0.61; 'simple': 0.61; "you'll": 0.62; "you've": 0.63; 'guarantee': 0.63; 'such': 0.63; 'chance': 0.65; 'note:': 0.66; 'mar': 0.68; 'date,': 0.68; 'records.': 0.68; 'wish': 0.70; 'transfer': 0.82; "'if": 0.84; 'safe;': 0.84; 'careful': 0.91; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=WVbqXu7dvNx0yhg3iA0SiAmhsuRqYU9WMfo/mDUqeG4=; b=cCfk78Bi8ZYweLfoO2yaQtMxFjJg46m6t4zE/x+DRMU0KM/6eF3uc1AWQfcMwaDEfZ OUr77Ce8I0zmJSH+6m4ijGnrmTJjkZWb+Qxcn5TxWpVZ2IYAVfOp3jCVZfZ3D0+lViJq ikohuf5vOvUh5TeawCZZ43M3a71HA01bnEbpSxCvfvyqNYp9iiteV1/Bbke99wImdhDL /cbTxZnJC0O2fKe1nu4awqU5xTwVYGFQVVi+1QYDTYy7rnP2NGD7FhOl/EOQ//QkLnfG 62BfXF9b7KjPnZVP1sj3UlNxZTAnzX4k8uCTG347ug5faQFiIYlhBTHa0uDwTqSk92H+ ABFA==
MIME-Version 1.0
X-Received by 10.220.109.145 with SMTP id j17mr27527079vcp.34.1364487304408; Thu, 28 Mar 2013 09:15:04 -0700 (PDT)
In-Reply-To <73c4a128-1783-4501-ab4b-c2c5c9017e3e@googlegroups.com>
References <73c4a128-1783-4501-ab4b-c2c5c9017e3e@googlegroups.com>
Date Fri, 29 Mar 2013 03:15:04 +1100
Subject Re: Replacing old data with new data using python
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3899.1364487306.2939.python-list@python.org> (permalink)
Lines 48
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1364487306 news.xs4all.nl 6920 [2001:888:2000:d::a6]:47451
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:42165

Show key headers only | View raw


On Fri, Mar 29, 2013 at 2:23 AM,  <inkprs@gmail.com> wrote:
> I think we can use it in 'if else' statement. something pseudocode like: if there is data in TBL2 for  date_sub(curdate(), interval 1 day), remove the database data and insert new data. else insert new data into database.
>
> How can I do it?

Can you simply do a searched DELETE?

DELETE from TBL2 WHERE date>date_sub(curdate(), interval 1 day)

That will happily do nothing if there are no such records. Be careful
of what it'll do, of course. Make sure you won't accidentally delete
too much!

(BTW, isn't "date" a reserved word? Maybe it isn't in MySQL.)

>         insertStatement = ("insert into TBL2( date, custId, userId) values ('%s', %d, %d)" % (date, custId, userId))
>         cur.execute(insertStatement)
>         con.commit()

I recommend you get used to parameterized queries. Assuming your date
field, coming from the other table, is clean, this will be safe; but
if there's any chance that date might have an apostrophe in it, this
code is very dangerous.

But there's a really neat trick you can do. If you have a guarantee
that all the SQL statements follow the structure you've given, just
prepend a simple string to them, thus:

"select date, pId as custId, tId as userId from TBL3"
-->
"insert into TBL2 (date, custId, userId) select date, pId as custId,
tId as userId from TBL3"

That'll do the whole transfer in a single statement! Something like this:

cur.execute("select nSql from TBL1")
for outerrow in cur.fetchall():
    cur.execute("insert into TBL2 (date, custId, userId) "+outerrow[0])
con.commit()

Note: I've backtabbed the commit() call so that the whole job happens
in a single transaction. You may wish to reindent it, to preserve the
semantics of your previous version; but I recommend doing the whole
job as one transaction, rather than committing each row separately. If
this job is interrupted, you'll have to start over anyway, so you may
as well have the database be clean.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Replacing old data with new data using python inkprs@gmail.com - 2013-03-28 08:23 -0700
  Re: Replacing old data with new data using python Chris Angelico <rosuav@gmail.com> - 2013-03-29 03:15 +1100

csiph-web