简体   繁体   English

如何使用python插入mysql数据库

[英]how to insert in mysql database using python

I'm writing a python script that allows insertion into the database mysql. 我正在写一个python脚本,它允许插入到数据库mysql中。 It gives me no error and "select * "Python code gives me the list I posted, but when I write in the terminal mysql -u root -p and select * it gives me the lines that I put in the database from the terminal only – the line that I put from the code it's not exist. 它没有错误,并且“ select *” Python代码提供了我发布的列表,但是当我在终端中编写mysql -u root -pselect *它仅给出了我从终端放入数据库的行–我从代码中插入的那行不存在。

Please can you help me. 请你帮我一下。 Thanks. 谢谢。

cur.execute("INSERT INTO ALL_DEVICES(HOST_NAME,ADDRESS_MAC,ADDRESS_IP,TIME_SCAN,DATE,ETAT) VALUES(%s,%s,%s,%s,%s,%s)",(host_name1, address_mac1,address_ip1,time_scan1,date1,etat))   

cur.execute("SELECT * FROM ALL_DEVICES")

rows = cur.fetchall()

print rows 

the result is: 结果是:

((2L, 'LKK', 'LLKK', 'KLK', 'KK', 'KJH', 'KLO'), (4L, 'LKK', 'LLKK', 'KLK', 'KK', 'KJH', 'KLO'), (5L, 'host_name1', 'address_mac1', 'address_ip1', 'tim', 'da', 'etat'), (9L, 'host_name1', 'address_mac1', 'address_ip1', 'tim', 'da', 'etat'), (19L, '', 'D8:3C:69:AA:02:E9', '192.168.1.3', '14:07', '21/03/15', 'Interdit'), (20L, '', 'D8:3C:69:AA:02:E9', '192.168.1.2', '4:07', '21/03/15', 'Interdit'), (21L, '', '2C:E4:12:54:7D:4F', '192.168.1.1', '14:07', '21/03/15', 'Interdit'))

and select * in the terminal give me: 然后在终端中选择*给我:

mysql> select * from ALL_DEVICES;

| Id | HOST_NAME  | ADDRESS_MAC  | ADDRESS_IP  | TIME_SCAN | DATE | ETAT |
+----+------------+--------------+-------------+-----------+------+------+
|  2 | LKK        | LLKK         | KLK         | KK        | KJH  | KLO  |
|  4 | LKK        | LLKK         | KLK         | KK        | KJH  | KLO  |
|  5 | host_name1 | address_mac1 | address_ip1 | tim       | da   | etat |
|  9 | host_name1 | address_mac1 | address_ip1 | tim       | da   | etat |
+----+------------+--------------+-------------+-----------+------+------+
4 rows in set (0,00 sec)

You have to use .commit() . 您必须使用.commit()

Let

conn = connect("database")
cur = conn.cursor()
cur.execute("INSERT into ...")  #insert query

conn.commit() #you missed it

cur.execute("SELECT ...") #select query

What itzmeontv said is correct. itzmeontv说的是正确的。 Unless you commit(), your changes to the DB will not be saved. 除非您commit(),否则对数据库的更改将不会保存。

Another way to interface with sql databases from Python is to use the "sqlalchemy" module. 从Python与sql数据库进行交互的另一种方法是使用“ sqlalchemy”模块。 This is the general flow: 这是一般流程:

from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

 database = "sqlite:////" + path + "/" + db_file
 self.engine = create_engine(database)
 Session = sessionmaker()
 Session.configure(bind=self.engine)
 self.session = Session()
 self.base = Base

# An example Table called train_info
class Traininfo(Base):
    __tablename__ = "train_info"
    train_name =   Column(String(250), primary_key=True)
    from_station = Column(String(150))
    to_station =   Column(String(150))
    direction =    Column(String(2))
    mode =         Column(String(2))
    date =         Column(String(64))

# Creating table and saving records in the table.
train_info = Traininfo(train_name = train_record[0],
                       from_station=train_record[1],
                       to_station=train_record[2],
                       direction=str(train_record[3]),
                       mode=str(train_record[4]),
                       date = train_record[5])
train_info.__table__.create(self.engine, checkfirst = True)

self.session.add(train_info)
self.session.commit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM