简体   繁体   中英

how to insert in mysql database using python

I'm writing a python script that allows insertion into the database 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.

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() .

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. Unless you commit(), your changes to the DB will not be saved.

Another way to interface with sql databases from Python is to use the "sqlalchemy" module. 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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