而从excel导入,+符号未插入数字字段,django
我有一个具有SMS-NO和WhatsApp-No字段的Excel文件。我们正在数据库中使用国家代码存储数字。虽然导入.xls文件编号已成功插入,但“+”符号未插入数据库中。
我正在共享Excel文件的屏幕截图以及如何将其存储在数据库中。
db
添加联系人选项
这是我的ViewS.py
fs = FileSystemStorage()
filedata = fs.save("ImportDB.xls", myfile)
BASE_DIR = os.path.dirname(
os.path.dirname(os.path.abspath(__file__)))
filepath = BASE_DIR + "\media\ImportDB.xls"
book = xlrd.open_workbook(filepath)
sheet = book.sheet_by_name("Sheet1")
database = MySQLdb.connect(
host="localhost", user="root", passwd="", db="mbs")
cursor = database.cursor()
query = """INSERT INTO addcontact (f_name, l_name, add_ln1, add_ln2, country, city, zip, sms_no, whtsp_no) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"""
for r in range(1, sheet.nrows):
f_name = sheet.cell(r, 0).value
l_name = sheet.cell(r, 1).value
add_ln1 = sheet.cell(r, 2).value
add_ln2 = sheet.cell(r, 3).value
country = sheet.cell(r, 4).value
city = sheet.cell(r, 5).value
zip = sheet.cell(r, 6).value
sms_no = sheet.cell(r, 7).value
whtsp_no = sheet.cell(r, 8).value
values = (f_name, l_name, add_ln1, add_ln2, country, city, zip, sms_no, whtsp_no)
cursor.execute(query, values)
cursor.close()
database.commit()
database.close()
fs.delete(filedata)
成功插入的每个数据。但是在数据库中添加了没有 +符号的数字。
I have one excel file which has SMS-no and WhatsApp-no fields. We are storing numbers with country codes in our database. While importing .xls file number is inserted successfully but the '+' sign is not inserted in the database.
I am sharing a screenshot of the excel file and how it is stored in a database.
Screenshot of value store in DB
Add contact option
Data store in DB through Add Contact option
Here is my code of views.py
fs = FileSystemStorage()
filedata = fs.save("ImportDB.xls", myfile)
BASE_DIR = os.path.dirname(
os.path.dirname(os.path.abspath(__file__)))
filepath = BASE_DIR + "\media\ImportDB.xls"
book = xlrd.open_workbook(filepath)
sheet = book.sheet_by_name("Sheet1")
database = MySQLdb.connect(
host="localhost", user="root", passwd="", db="mbs")
cursor = database.cursor()
query = """INSERT INTO addcontact (f_name, l_name, add_ln1, add_ln2, country, city, zip, sms_no, whtsp_no) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"""
for r in range(1, sheet.nrows):
f_name = sheet.cell(r, 0).value
l_name = sheet.cell(r, 1).value
add_ln1 = sheet.cell(r, 2).value
add_ln2 = sheet.cell(r, 3).value
country = sheet.cell(r, 4).value
city = sheet.cell(r, 5).value
zip = sheet.cell(r, 6).value
sms_no = sheet.cell(r, 7).value
whtsp_no = sheet.cell(r, 8).value
values = (f_name, l_name, add_ln1, add_ln2, country, city, zip, sms_no, whtsp_no)
cursor.execute(query, values)
cursor.close()
database.commit()
database.close()
fs.delete(filedata)
Every data is inserted successfully. But number added without + sign in database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将字段从某些
Integerfield
更改为Chardfield
。数据库将整数+123
作为正数123
。You need to change field from some
IntegerField
toChardField
. Database treats integer+123
as simply positive number123
.我将数据转换为字符串和contrate +符号,但是在表达每个数字中,它在每个数字中附加了.0,因此我使用了拆分函数,并且仅获取值。 (点)。
这是我的代码
I converted data in string and concate + sign, but after concating it is appending .0 in each, number, So I used the split function and only get the value before. (dot).
here is my code