4 examples of 'python mysql insert' in Python

Every line of 'python mysql insert' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
251def insert(self, table, values, autoinc=False):
252 try:
253 cur = self.cur
254 mystr = ("INSERT INTO %s VALUES(" % table) + ','.join([`str(val)` for val in values]) + ")"
255
256 logger.debug(mystr)
257 cur.execute(mystr)
258 cur.connection.commit()
259 # Auto-increment?
260 if autoinc:
261 # Probably wrong!
262 cur.execute("SELECT LAST_INSERT_ID();")
263 result = cur.fetchone()
264 RID = result['LAST_INSERT_ID()']
265 return RID
266 except mdb.cursors.Error, e:
267 logger.error(e)
268 return False;
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

57def insert(self,sql,verbose=True):
58 if verbose:
59 print '----\ninsert sql:',sql
60 try:
61 self._cur.execute("SET NAMES utf8")
62 res = self._cur.execute(sql)
63 self._conn.commit()
64 except Exception,e:
65 err_msg = '[-] Insert err: ',e.args[0],e.args[1]
66 res = False
67 print err_msg
68 # res = self._conn.insert_id()
69 res = self._cur.lastrowid
70 # print res
71 return res
59def insert(self, table, params) :
60 """
61 table : name of the table
62 params : dictionnary that will be used as in cursor.execute(sql,params)
63
64 >>> s = SQLGenerator()
65 >>> s.insert('test',{'nom':'dupont'})
66 'INSERT INTO test ( nom ) VALUES ( %(nom)s )'
67 >>> s.insert('test',{'nom':'dupont','prenom':'jean'})
68 'INSERT INTO test ( nom, prenom ) VALUES ( %(nom)s, %(prenom)s )'
69 """
70 keys = ', '.join(params.keys())
71 values = ', '.join(["%%(%s)s" % x for x in params])
72 sql = 'INSERT INTO %s ( %s ) VALUES ( %s )' % (table, keys, values)
73 return sql
118def insert(self, table, data):
119 # 转义
120 sql = "INSERT INTO %s(data) VALUES('%s') " % (table, data.replace("'", r"\'").replace(r"\n", r'\\n').
121 replace(r'\"', r'').replace(r'\\', r'/'))
122 logger.info(sql)
123 try:
124 self.cursor.execute(sql)
125 self.conn.commit()
126 return True, self.cursor.rowcount
127 except Exception as e:
128 logger.error("Insert error: %s" % e)
129 self.conn.rollback()
130 return False, str(e)

Related snippets