使用python连接mysql有两种方法,
1
import mysql.connector
cnx = mysql.connector.connect(user='scott', password='tiger',host='127.0.0.1',database='employees')
cnx.close()
Run Code Online (Sandbox Code Playgroud)
2
import MySQLdb
db = MySQLdb.connect(host="localhost", # your host, usually localhost
user="john", # your username
passwd="megajonhy", # your password
db="jonhydb") # name of the data base
cur = db.cursor()
cur.execute("SELECT * FROM YOUR_TABLE_NAME")
Run Code Online (Sandbox Code Playgroud)
我不知道MySQLdb和mysql连接器之间的区别,我何时应该使用MySQLdb,何时应该使用mysql连接器?请告诉我,非常感谢.
我正在研究 的http 源代码Golang,我发现了这个
func NewRequestWithContext(ctx context.Context, method, url string, body io.Reader) (*Request, error) {
...
rc, ok := body.(io.ReadCloser)
...
}
Run Code Online (Sandbox Code Playgroud)
但这body是nil,它是通过以下方式传递的
func NewRequest(method, url string, body io.Reader) (*Request, error) {
return NewRequestWithContext(context.Background(), method, url, body)
}
func (c *Client) Get(url string) (resp *Response, err error) {
req, err := NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
return c.Do(req)
}
Run Code Online (Sandbox Code Playgroud)
函数Get传递nil给函数NewRequest, …
我有一个很长的stringfrom Base64,但是它太长了,我如何将其转换为短的。我希望这是一个可逆的操作,因为我想从短的中得到长的。顺便说一句,我不想将这两个字符串保存到数据库中。