1. 程式人生 > >django 和 mysql 的連線 MySQL 查看錶結構簡單命令

django 和 mysql 的連線 MySQL 查看錶結構簡單命令

現在資料庫裡先建庫save

CREATE DATABASE save;

use save;

再建表

create table money

import pymysql


# 連線資料庫
connect = pymysql.Connect(
host='localhost',
port=3306,
user='root',
passwd='1234',
db='save',
charset='utf8'
)

# 獲取遊標
cursor = connect.cursor()

# 插入資料
sql = "INSERT INTO money (name, account, saving) VALUES ( '%s', '%s', %.2f )"
data = ('雷軍', '13512345678', 10000)
cursor.execute(sql % data)
connect.commit()
print('成功插入', cursor.rowcount, '條資料')

# 修改資料
sql = "UPDATE money SET saving = %.2f WHERE account = '%s' "
data = (8888, '13512345678')
cursor.execute(sql % data)
connect.commit()
print('成功修改', cursor.rowcount, '條資料')

# 查詢資料
sql = "SELECT name,saving FROM money WHERE account = '%s' "
data = ('13512345678',)
cursor.execute(sql % data)
for row in cursor.fetchall():
print("Name:%s\tSaving:%.2f" % row)
print('共查找出', cursor.rowcount, '條資料')

# 刪除資料
sql = "DELETE FROM money WHERE account = '%s' LIMIT %d"
data = ('13512345678', 1)
cursor.execute(sql % data)
connect.commit()
print('成功刪除', cursor.rowcount, '條資料')

# 事務處理
sql_1 = "UPDATE money SET saving = saving + 1000 WHERE account = '18012345678' "
sql_2 = "UPDATE money SET expend = expend + 1000 WHERE account = '18012345678' "
sql_3 = "UPDATE money SET income = income + 2000 WHERE account = '18012345678' "

try:
cursor.execute(sql_1) # 儲蓄增加1000
cursor.execute(sql_2) # 支出增加1000
cursor.execute(sql_3) # 收入增加2000
except Exception as e:
connect.rollback() # 事務回滾
print('事務處理失敗', e)
else:
connect.commit() # 事務提交
print('事務處理成功', cursor.rowcount)

# 關閉連線
cursor.close()

connect.close()

修改view.py裡的檔案

原網址 http://blog.csdn.net/johline/article/details/69549131

一、簡單描述表結構,欄位型別

desc tabl_name;

顯示錶結構,欄位型別,主鍵,是否為空等屬性,但不顯示外來鍵。

例如:desc table_name

二、查詢表中列的註釋資訊

select * from information_schema.columns
where table_schema = 'db' #表所在資料庫
and table_name = 'tablename' ; #你要查的表

例如:

可以自動選擇你需要資訊

三、只查詢列名和註釋
select column_name, column_comment from information_schema.columns where table_schema ='db' and table_name = 'tablename' ;

例如:

四、#查看錶的註釋
select table_name,table_comment from information_schema.tables where table_schema = 'db' and table_name ='tablename'

例如:


五、查看錶生成的DDL

show create table table_name;

例如:

這個命令雖然顯示起來不是太容易看, 這個不是問題可以用\G來結尾,使得結果容易閱讀;該命令把建立表的DDL顯示出來,於是表結構、型別,外來鍵,備註全部顯示出來了。

我比較喜歡這個命令:輸入簡單,顯示結果全面。

補充一些可能用到的命令:

建表命令:
CREATE TABLE `t_sold_order` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`dt` date DEFAULT NULL COMMENT '日期',
`hour` tinyint(2) DEFAULT '0' COMMENT '小時',
`hour_order` int(11) DEFAULT '0' COMMENT '小時訂單數',
`total_order` int(11) DEFAULT '0' COMMENT '總的訂單數',
`prediction` int(11) DEFAULT '0' COMMENT '預測訂單數',
PRIMARY KEY (`id`),
UNIQUE KEY `dt_hour` (`dt`,`hour`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='實時訂單數'

表操作命令:
複製表結構:create table table1 like table;
複製資料:insert into table1 select * from table

機器授權:
grant select on *.* to 'reader'@'%' identified by '123456' WITH GRANT OPTION
flush privileges

查詢資料直接插入
insert into t_visual_user_domain(`user_id`,`domain`,`group`) select id,'www.baidu.com' as domain,`group` from t_visual_user;

修改表結構
alter table competitor_goods add sku_id bigint(20) unsigned DEFAULT NULL COMMENT '商品銷售碼';


相關推薦

django mysql連線 MySQL 結構簡單命令

現在資料庫裡先建庫save CREATE DATABASE save; use save; 再建表 create table money import pymysql # 連線資料庫 connect = pymysql.Connect( host='loca

MySQL 結構簡單命令

一、簡單描述表結構,欄位型別 desc tabl_name; 顯示錶結構,欄位型別,主鍵,是否為空等屬性,但不顯示外來鍵。 二、查詢表中列的註釋資訊 select * from information_schema.columns where table_schema

MySQL 結構簡單命令

一、簡單描述表結構,欄位型別 desc tabl_name; 顯示錶結構,欄位型別,主鍵,是否為空等屬性,但不顯示外來鍵。 例如:desc table_name 二、查詢表中列的註釋資訊 select * from information_schema.columnswhere table_schema =

mysql中如何結構

1.登陸mysql 命令: mysql -uroot -p 2.此處以mysql資料庫的func表為例 查看錶結構的方法1 ---命令: desc func; --方法2 命令: describe func; --方法3 命令:

MySQL 結構簡單命令

flush 生成 直接插入 info 表結構 not 簡單 with utf 一、簡單描述表結構,字段類型 desc tabl_name; 顯示表結構,字段類型,主鍵,是否為空等屬性,但不顯示外鍵。 例如:desc table_name 二、查詢表中列的註釋信息 sele

MySQL結構語句結構的兩種方法

http://blog.csdn.net/number1killer/article/details/77841565 http://blog.csdn.net/number1killer/article/details/77878047 http:/

mysql命令結構,欄位等資訊 TRUNCATE TABLE

mysql查看錶結構命令,如下: desc 表名; show columns from 表名; describe 表名; show create table 表名; use information_schema select * from columns where tab

mysql結構查詢註釋

//查詢表結構select t.column_name,t.column_comment,t.column_type,case t.column_key when 'pri' then 'yes' else 'no' end,t.is_nullable,t.column_co

mysql結構資訊

需求背景是給一個表名然後給出相應的表結構資訊及索引資訊 常用的命令有如下: 1. desc tableName; desc employees.employees; 2. show columns from tableName; show COLUMNS from employees.employe

mysql結構資訊需求背景是給一個表名然後給出相應的表結構資訊及索引資訊 常用的命令有如下: 1. desc tableName; desc employees.employees; 2. sh

需求背景是給一個表名然後給出相應的表結構資訊及索引資訊 常用的命令有如下: 1. desc tableName; desc employees.employees; 2. show columns from tableName; show COLUMNS from employees.employe

資料庫MySQL之如何?

資料庫MySQL之如何查看錶? 檢視資料表列表語法 SHOW TABLES [FROM db_name] [LIKE 'pattern' | WHERE expr] 1. 檢視當前資料庫中的表 SHOW TABLES; 2. FROM之後,當前資

mysql結構相關sql

/**查看錶結構**/ desc yourtablename /**檢視建立表語句**/ show create table yourtablename /**檢視所有列的資訊**/ use informa

mysql結構的幾種方式

在實際的開發中,我們肯定經常要查看錶結構的,特別是遇到自己不是最初開發的專案的時候,通過表結構,大概就能看出表裡存什麼資料,每個欄位代表什麼意思。實際上有很多查看錶結構的方式,下面就拿rails裡面的表schema_migrations介紹一下我自己常用的命令: desc

MySQL結構方法整理

在Mysql的shell命令列下查詢表的結構: 1.desc(描述)命令 desc tablename; describe tablename;2.show命令 show columns from t

【Linux】mysql命令結構,欄位等資訊

mysql查看錶結構命令,如下: desc table_name; //查表的欄位資訊(不包含欄位內容) show columns from table_name; //同上 show create table table_name; //查表字段資訊和字符集資訊

mysql命令結構,欄位等資訊

mysql查看錶結構命令,如下: desc 表名; show columns from 表名; describe 表名; show create table 表名; use information_schema select * from columns where tab

MySQL結構及檢視建表語句

查看錶結構:desc 表名 mysql> use recommend; Database changed mysql> desc user; +--------------+--------------+------+-----+--------

django Navicat 連線 MYSQL 8.0遠端連線

MYSQL 8.0內新增加mysql_native_password函式,通過更改這個函式密碼來進行遠端連線。 更改ROOT使用者的native_password密碼,直接用ROOT使用者的賬號密碼去連線是不行,即時密碼正確。 mysql> ALTER

【數據庫】】MySQL之desc結構的詳細信息

類型 dex des varchar 其它 定義 creat eat desc 在mysql中如果想要查看表的定義的話;有如下方式可供選擇   1、show create table 語句: show create table table_name;   2、desc

Oracle (01)Oracle資料庫的安裝步驟.搭建上課所用的資料庫環境.table (二維表).結構.資料庫中常用的資料型別

Oracle 01 目錄 Oracle資料庫的安裝步驟 搭建上課所用的資料庫環境 table (二維表) ***** 查看錶結構 ***** 資料庫中常用的資料型別 ***** SQL語句 ***** where子句 ***** 對於null值 如何進行判斷 ***** 模糊查詢 *