SQL基础语法及python操作mysql(笔记)

SQL基础语法:
1、SELECT :SELECT 列名称 FROM 表名称 以及 SELECT * FROM 表名称
2、DISTINCT:SELECT DISTINCT 列名称 FROM 表名称
3、WHERE:SELECT 列名称 FROM 表名称 WHERE 列 运算符 值
4、ORDER BY 语句用于根据指定的列对结果集进行排序。默认升序:ASC,降序:DESC
5、INSERT:INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....),其中列名可以省略
6、UPDATE:UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值
7、DELETE:DELETE FROM 表名称 WHERE 列名称 = 值 (用于删除列表中一行)
8、ALTER TABLE 语句用于在已有的表中添加、修改或删除列。
1、如需在表中添加列,请使用下列语法:
ALTER TABLE table_name ADD column_name datatype
2、要删除表中的列,请使用下列语法:
ALTER TABLE table_name DROP COLUMN column_name

编码错误解决:UnicodeEncodeError: 'latin-1' codec can't encode characters in position
    1. conn.set_character_set('utf8')  
    2.    curs.execute('set names utf8')  
    3.    curs.execute('SET CHARACTER SET utf8;')  
    4.    curs.execute('SET character_set_connection=utf8;') 


创建数据库 create database if not exists python charset=utf8;

查看字符集:1 show variables like 'collation_%';
2 show variables like 'character_set_%';

1.修改数据库字符集 alter database mini default character set = utf8;

2.创建数据库设置字符集 4 create database mydb character set utf8;
修改单个表单字符集
1 alter table pub_logs default character set = gb2312; 2 alter table pub_logs convert to character set gb2312;

1、创建数据库的时候:
  CREATE DATABASE `test`
  CHARACTER SET 'utf8'
  COLLATE 'utf8_general_ci';
   2、建表的时候
  CREATE TABLE `table_name` (
  `ID` varchar(40) NOT NULL default primary key auto_increment '',
  `UserID` varchar(40) NOT NULL default '',
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 MySql = "create table players(id int primary key auto_increment,name char(10),age int,gender char(6))"


一次性插入多条
insert_memory_sql = "insert into memory(available,percent,total,used,free) values(%s,%s,%s,%s,%s)"
params1 =(eval(memInfo.value)['available'],eval(memInfo.value)['percent'],eval(memInfo.value)['total'],eval(memInfo.value)['used'],eval(memInfo.value)['free'])
 

import MySQLdb

conn= MySQLdb.connect(
        host='localhost',
        port = 3306,
        user='root',
        passwd='123456',
        db ='test',
        )
cur = conn.cursor()

#创建数据表
#cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10))")

#插入一条数据
#cur.execute("insert into student values('2','Tom','3 year 2 class','9')")


#修改查询条件的数据
#cur.execute("update student set class='3 year 1 class' where name = 'Tom'")

#删除查询条件的数据
#cur.execute("delete from student where age='9'")

cur.close()
conn.commit()
conn.close()
 
原文地址:https://www.cnblogs.com/WhatTTEver/p/6745236.html