行继续符后面的语法错误意外字符

时间:2017-01-06 10:31:47

标签: python connection

有人可以告诉我程序中有什么问题吗?当我运行此程序时,我收到以下错误消息:

  行继续符后面的

syntaxerror意外字符

import sqlite3
sqlite_file = 'my_first_db.sqlite'   # NAME OF THE SQL DATABASE FILE
table_name1 = 'my_table_1'  # NAME OF THE TABLE THAT TO BE CREATED.
table_name2 = 'my_table_2'  # NAME OF THE SECOND TABLE THAT TO BE CREATED.
new_filed = 'my_1st_coulmn' # NAME OF THE COULMN
filed_type = 'INTEGER'      # COULMN DATA TYPE
# CONNECTING TO DATA BASE FILE
conn = sqlite3.connect(sqlite_file)
c =  conn.cursor()
# CREATEING NEW SQLITE TABLE WITH 1 COULMN
c.execute('create table {tn} ({nf}) {ft})'\ .format(tn=table_name1,nf=new_filed,ft=filed_type))
# Creating a second table with 1 column and set it as PRIMARY KEY
# note that PRIMARY KEY column must consist of unique values!
c.execute('create table {tn} ({nf}) {ft} primary key)'\.format(tn=table_name2,nf=new_filed,ft=filed_type))
# Committing changes and closing the connection to the database file
conn.commit()
conn.close()

1 个答案:

答案 0 :(得分:0)

在编写长查询时,

\用于行继续。 因此,如果您继续执行代码,则在创建和执行查询时删除\ before .format()。

  # CREATEING NEW SQLITE TABLE WITH 1 COULMN
    c.execute('create table {tn} ({nf}) {ft})'.format(tn=table_name1,nf=new_filed,ft=filed_type))

有关详细信息,请阅读此内容。https://pyformat.info/