Python MySQL详细教程
MySQL是一个常用的关系型数据库管理系统,用于存储和管理数据。在Python中,我们可以使用MySQL Connector模块来连接和操作MySQL数据库。这篇文章将向您介绍如何在Python中使用MySQL Connector模块。
安装MySQL Connector模块
在您开始使用MySQL Connector模块之前,您必须先安装它。您可以使用以下命令在终端中安装MySQL Connector模块。
```
pip install mysql-connector-python
```
连接MySQL数据库
连接MySQL数据库需要几个必要的参数,如主机名、数据库名、用户名和密码。在Python中使用MySQL Connector模块时,您可以使用以下代码连接MySQL数据库。
```Python
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
print(mydb)
```
上面的代码将打印连接对象,如果连接成功,它将打印一个类似于“ 创建数据库 如果您需要创建一个新的数据库,您可以使用以下代码。 ```Python import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword" ) mycursor = mydb.cursor() mycursor.execute("CREATE DATABASE mydatabase") ``` 上面的代码将创建一个名为“mydatabase”的新数据库。 创建表 在您可以向MySQL数据库中添加数据之前,您必须先创建一个表。您可以使用以下代码向MySQL数据库中添加一个新表。 ```Python import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="mydatabase" ) mycursor = mydb.cursor() mycursor.execute("CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))") ``` 上面的代码将创建一个名为“customers”的新表,并包含两个列:name和address。name列和address列都是VARCHAR数据类型,长度为255个字符。 插入数据 一旦您创建了一个表,您就可以向其中添加数据。您可以使用以下代码向MySQL数据库中添加一条新记录。 ```Python import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="mydatabase" ) mycursor = mydb.cursor() sql = "INSERT INTO customers (name, address) VALUES (%s, %s)" val = ("John Smith", "123 Main St.") mycursor.execute(sql, val) mydb.commit() print(mycursor.rowcount, "record inserted.") ``` 上面的代码将插入一条名为John Smith的新记录,并将其地址设置为123 Main St。在插入数据后,我们必须使用commit()方法提交更改。 查询数据 查询表中的数据是一个常见的任务,您可以使用以下代码在MySQL数据库中查询记录。 ```Python import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="mydatabase" ) mycursor = mydb.cursor() mycursor.execute("SELECT * FROM customers") myresult = mycursor.fetchall() for x in myresult: print(x) ``` 上面的代码将查询名为“customers”的表中的所有记录,并使用for循环打印每一行。 更新数据 您可以使用以下代码在MySQL数据库中更新记录。 ```Python import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="mydatabase" ) mycursor = mydb.cursor() sql = "UPDATE customers SET address = '321 Main St.' WHERE name = 'John Smith'" mycursor.execute(sql) mydb.commit() print(mycursor.rowcount, "record(s) affected") ``` 上面的代码将搜索名为“John Smith”的记录,并将其地址更新为321 Main St。在更新记录后,我们必须使用commit()方法提交更改。 删除记录 您可以使用以下代码从MySQL数据库中删除记录。 ```Python import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="mydatabase" ) mycursor = mydb.cursor() sql = "DELETE FROM customers WHERE name = 'John Smith'" mycursor.execute(sql) mydb.commit() print(mycursor.rowcount, "record(s) deleted") ``` 上面的代码将搜索名为“John Smith”的记录,并将其从名为“customers”的表中删除。 这是一个简短的介绍,如何使用Python中的MySQL Connector模块来连接、操作和管理MySQL数据库。通过使用这些示例代码,您可以开始使用MySQL Connector模块来开发自己的MySQL数据库应用程序。 Python内置函数处理字符串 字符串是编程中最常用的变量类型之一,Python中有很多内置函数可以帮助我们处理字符串。下面是一些常用的Python内置函数,用于处理字符串。 1. len()函数 len()函数可用于返回字符串的长度。它输出字符串中包含的字符数量。 ```Python string = "This is a string." print(len(string)) ``` 输出: ``` 18 ``` 2. count()函数 count()函数可用于计算字符串中出现指定子字符串的次数。 ```Python string = "This is a string." substring = "is" print(string.count(substring)) ``` 输出: ``` 2 ``` 3. find()函数 find()函数可用于查找字符串中指定子字符串的位置。 ```Python string = "This is a string." substring = "is" print(string.find(substring)) ``` 输出: ``` 2 ``` 4. upper()函数 upper()函数可用于将字符串转换为大写。 ```Python string = "This is a string." print(string.upper()) ``` 输出: ``` THIS IS A STRING. ``` 5. lower()函数 lower()函数可用于将字符串转换为小写。 ```Python string = "This is a string." print(string.lower()) ``` 输出: ``` this is a string. ``` 6. replace()函数 replace()函数可用于替换字符串中指定的子字符串。 ```Python string = "This is a string." old_substring = "is" new_substring = "was" print(string.replace(old_substring, new_substring)) ``` 输出: ``` Thwas was a string. ``` 7. split()函数 split()函数可用于将字符串分割成一个列表,使用指定的分隔符。 ```Python string = "This is a string." separator = " " print(string.split(separator)) ``` 输出: ``` ['This', 'is', 'a', 'string.'] ``` 以上是Python中一些常用的字符串函数,希望这篇文章能够为您提供帮助! 如果你喜欢我们三七知识分享网站的文章,
欢迎您分享或收藏知识分享网站文章
欢迎您到我们的网站逛逛喔!https://www.ynyuzhu.com/
如果,彼此在在最美的年华遇见,深深相爱,携手到老,那该多好。生前能相依共处,愿死后得并葬丘荒。能有如此深爱,人生已是无憾。