有没有mysql上面操作的代码
时间: 2023-09-17 18:06:14 浏览: 44
### 回答1:
是的,以下是一些MySQL操作的示例代码:
连接到MySQL数据库:
```python
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword"
)
print(mydb)
```
创建数据库:
```python
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE mydatabase")
```
创建表格:
```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))")
```
插入数据:
```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", "Highway 21")
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "record inserted.")
```
查询数据:
```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)
```
更新数据:
```python
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "UPDATE customers SET address = 'Canyon 123' WHERE name = 'John'"
mycursor.execute(sql)
mydb.commit()
print(mycursor.rowcount, "record(s) affected")
```
删除数据:
```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'"
mycursor.execute(sql)
mydb.commit()
print(mycursor.rowcount, "record(s) deleted")
```
### 回答2:
当然有,以下是常用的一些MySQL数据库操作的代码:
1. 创建数据库:
CREATE DATABASE 数据库名;
2. 删除数据库:
DROP DATABASE 数据库名;
3. 使用数据库:
USE 数据库名;
4. 创建数据表:
CREATE TABLE 表名 (
列名1 数据类型,
列名2 数据类型,
...
);
5. 删除数据表:
DROP TABLE 表名;
6. 插入数据:
INSERT INTO 表名 (列名1, 列名2, ...) VALUES (值1, 值2, ...);
7. 更新数据:
UPDATE 表名 SET 列名1 = 值1, 列名2 = 值2, ... WHERE 条件;
8. 删除数据:
DELETE FROM 表名 WHERE 条件;
9. 查询数据:
SELECT 列名1, 列名2, ... FROM 表名 WHERE 条件;
10. 添加索引:
CREATE INDEX 索引名 ON 表名 (列名);
11. 删除索引:
DROP INDEX 索引名 ON 表名;
以上是一些常见的MySQL数据库操作的代码示例,可以根据实际需求进行相应的修改和扩展。
### 回答3:
是的,以下是一些常见的MySQL数据库操作代码:
1. 连接数据库:
```python
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="username",
password="password",
database="database_name"
)
```
2. 创建表:
```python
mycursor = mydb.cursor()
mycursor.execute("CREATE TABLE customers (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT)")
```
3. 插入数据:
```python
mycursor = mydb.cursor()
sql = "INSERT INTO customers (name, age) VALUES (%s, %s)"
val = ("John", 25)
mycursor.execute(sql, val)
mydb.commit()
```
4. 查询数据:
```python
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM customers")
result = mycursor.fetchall()
for row in result:
print(row)
```
5. 更新数据:
```python
mycursor = mydb.cursor()
sql = "UPDATE customers SET age = 30 WHERE name = 'John'"
mycursor.execute(sql)
mydb.commit()
```
6. 删除数据:
```python
mycursor = mydb.cursor()
sql = "DELETE FROM customers WHERE id = 1"
mycursor.execute(sql)
mydb.commit()
```
以上是一些常见的MySQL数据库操作代码示例,可以根据具体需求进行修改和扩展。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![dmg](https://img-home.csdnimg.cn/images/20210720083646.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![dmg](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)