数据库varchar 相当于python里的哪个类型

如题所述

MySQL数据库的varchar类型数据在Python中是str类型。

#coding=utf-8
import MySQLdb

conn= MySQLdb.connect(
        host='192.xxx.xxx.xxx',
        port = 3306,
        user='root',
        passwd='PASSWORD',
        db ='testDB',
        )
cur = conn.cursor()

datanum=cur.execute("select DiskId  from test_table;")
print datanum

info = cur.fetchmany(datanum)
for temp in info:
    print temp[0]
    
print type(temp[0])       #打印类型 

cur.close()
conn.commit()
conn.close()

数据库示例:

mysql> select *  from test_table;
+----+--------+------+------+-------+-------+------+
| Id | DiskId | Name | Sg   | PhyId | Model | SN   |
+----+--------+------+------+-------+-------+------+
|  1 | 1      | NULL | NULL | NULL  | NULL  | NULL |
|  2 | 2      | NULL | NULL | NULL  | NULL  | NULL |
|  3 | 3      | NULL | NULL | NULL  | NULL  | NULL |
|  4 | 4      | NULL | NULL | NULL  | NULL  | NULL |
|  5 | 5      | NULL | NULL | NULL  | NULL  | NULL |
|  6 | 6      | NULL | NULL | NULL  | NULL  | NULL |
|  7 | 7      | NULL | NULL | NULL  | NULL  | NULL |
|  8 | 8      | NULL | NULL | NULL  | NULL  | NULL |
+----+--------+------+------+-------+-------+------+
8 rows in set (0.00 sec)

mysql> describe test_table;
+------------+-------------+------+-----+---------+----------------+
| Field      | Type        | Null | Key | Default | Extra          |
+------------+-------------+------+-----+---------+----------------+
| Id         | int(11)     | NO   | PRI | NULL    | auto_increment |
| DiskId     | varchar(25) | YES  |     | NULL    |                |
| Name       | varchar(25) | YES  |     | NULL    |                |
| Sg         | varchar(25) | YES  |     | NULL    |                |
| PhyId      | varchar(25) | YES  |     | NULL    |                |
| Model      | text        | YES  |     | NULL    |                |
| SN         | text        | YES  |     | NULL    |                |
+------------+-------------+------+-----+---------+----------------+
13 rows in set (0.00 sec)

参考资料:http://www.cnblogs.com/fnng/p/3565912.html

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-05-03
所说所有的变量都是对象。 对象在python里,其实是一个指针,指向一个数据结构,数据结构里有属性,有方法。

对象通常就是指变量。从面向对象OO的概念来讲,对象是类的一个实例。在python里很简单,对象就是变量。

class A:
myname="class a"
上面就是一个类。不是对象
a=A()
这里变量a就是一个对象。
它有一个属性(类属性),myname,你可以显示出来
print a.myname

所以,你看到一个变量后面跟点一个小数点。那么小数点后面本回答被提问者采纳
第2个回答  2020-12-26

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网