question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

peewee mysql how to execute 'insert many on duplicate key update'

See original GitHub issue

Hi Charles, how to execute sql command like

insert into table(ip,hostname,...)
values
('','',...)
('','',...)
('','',...)
...
on duplicate key update hostname=values(hostname),

upsert and on_conflict does not work

In [49]: result
Out[49]: 
[{'country': '',
  'guid': '122.225.227.21:80',
  'head': '',
  'hostname': '122.225.227.21',
  'ip': '122.225.227.21',
  'ipr': '',
  'port': 80,
  'protocol_type': 'tcp',
  'province': '',
  'server': '',
  'title': ''},
...]
In [50]: basicinfo_ip_finger.insert_many(result).on_conflict('REPLACE').execute()

ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OR REPLACE INTO `basicinfo_ip_finger` (`ip`, `port`, `protocol_type`, `hostname`' at line 1")

please help, thanks

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
coleifercommented, Nov 20, 2019

Peewee does support ON DUPLICATE KEY UPDATE:

http://docs.peewee-orm.com/en/latest/peewee/querying.html#upsert

MySQL supports upsert via the ON DUPLICATE KEY UPDATE clause. For example:

class User(Model):
    username = TextField(unique=True)
    last_login = DateTimeField(null=True)
    login_count = IntegerField()

# Insert a new user.
User.create(username='huey', login_count=0)

# Simulate the user logging in. The login count and timestamp will be
# either created or updated correctly.
now = datetime.now()
rowid = (User
         .insert(username='huey', last_login=now, login_count=1)
         .on_conflict(
             preserve=[User.last_login],  # Use the value we would have inserted.
             update={User.login_count: User.login_count + 1})
         .execute())
3reactions
yangbhcommented, Nov 11, 2015

here is my poor solution

q=basicinfo_ip_finger.insert_many(result).sql()
db.execute_sql(q[0]+' on duplicate key update hostname=values(hostname),...', q[1])

any good idea? thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I insert multiple rows with INSERT REPLACE in ...
The problem is that some of the rows may already exist in my database. The solution should be to either ignore or replace...
Read more >
Querying — peewee 3.15.4 documentation
After executing the insert query, the primary key of the new row is returned. ... MySQL supports upsert via the ON DUPLICATE KEY...
Read more >
Build a MySQL upsert query to insert/update multiple records ...
Use transactions by initialising your cursors using with database.cursor() as cursor: You'll want to run your code through pep8 to improve ...
Read more >
peewee — pyknotid 0.5.4 documentation - Read the Docs
_columns else self.c for key, value in kwargs.items(): insert[getattr(src, ... SQL('='), v))) if updates: return NodeList((SQL('ON DUPLICATE KEY UPDATE'), ...
Read more >
peewee Documentation [image] - manpages.ubuntu!
When you call save(), peewee determines whether to do an INSERT versus an UPDATE based on ... MySQL supports upsert via the ON...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found