Steps to operate MySql in Python under Mac

Step 1: Install MySql: https://dev.mysql.com/downloads/mysql/5.6.html , download mysql-5.7.19-macos10.12-x86_64.dmg After
the installation is complete, a pop-up box will prompt the generated temporary password , you need to save it so that you can change the password later.
For example: A temporary password is generated for root@localhost: a!=ga&gH=7vF

Step 2: Modify the environment variable You
need to add the feasible file directory of mysql to the environment variable
echo $PATH, press Enter to execute the command to view the current variable value, here we will modify the PATH variable to test.
sudo vi ~/.bash_profile, press Enter to enter the password and use vi to open the bash_profile file in the user directory. Be sure to use sudo, otherwise you will not have permission to save the file.

Add the mysql directory to the environment variable. Modify the content in ~/.bash_profile as follows PATH=${PATH}:/usr/local/mysql-5.7.19-macos10.12-x86_64/bin
export PATH

Execute source ~/.bash_profile, the modification of environment variables will take effect immediately

Step 3: Change the password
Method 1:
Outside the mysql system, use the command mysqladmin
mysqladmin -u root -p password “test123”
Enter password: [Enter the original password]
Method 2:
Log in to the mysql system,
mysql -uroot -p
Enter password : [Enter the original password]
mysql>use mysql;
mysql> update user set password=password(“test”) where user='root';
mysql> flush privileges;
mysql> exit;

Step 4: Modify the mysql character encoding to utf-8
connection address: http://www.cnblogs.com/gerald-x/p/6913877.html

Step 5: Download the mysql driver for python: https://pypi.python.org/pypi/mysql-connector-python/2.0.4 , after mysql-connector-python
is downloaded, enter the directory, unzip, and install
tar -xvf MySQL -python-1.2.2.tar
cd MySQL-python-1.2.2
python setup.py build
python setup.py install

Step 6: Create a database: The following command simply demonstrates the process of creating a database, the data name is testBase:
[root@host]# mysqladmin -u root -p create testBase
Enter password: **

Step 5: Operate MySql with python code
"'
MySQL's SQL placeholder is %s;
usually we pass in use_unicode=True when connecting to MySQL, so that MySQL's DB-API always returns Unicode.
"'
Import MySQL driver:
import mysql. connector
Note set password to your root password:
conn = mysql.connector.connect(user='root', password='123456', database='testBase', use_unicode=True)
cursor = conn.cursor()
create user Table:
cursor.execute('create table user (id varchar(20) primary key, name varchar(20))')
inserts a row of records, note that the MySQL placeholder is %s:
cursor.execute('insert into user ( id, name) values ​​(%s, %s)', ['1', 'Michael'])
print cursor.rowcount
conn.commit()
cursor.close()
run query:
cursor = conn.cursor()
cursor.execute(‘select * from user where id = %s’, (‘1’,))
values = cursor.fetchall()
print values
print cursor.close()
conn.close()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325638270&siteId=291194637