在Django数据库中插入多个值-原始sqlite语句

时间:2018-08-21 16:07:53

标签: sql-server django python-3.x sqlite

我在Django中使用原始sql。

p.execute('''INSERT INTO webapp_information VALUES(login=%s, idd=%s, avatar_url=%s, gravatar_id=%s, url=%s, html_url=%s, followers_url=%s, following_url=%s, gists_url=%s, starred_url=%s, subscriptions_url=%s, organizations_url=%s, repos_url=%s, events_url=%s, received_events_url=%s,typ=%s,site_admin=%s, name=%s, company=%s, blog=%s, location=%s,email=%s, hireable=%s, bio=%s, public_repos=%s, public_gists=%s, followers=%s, following=%s, created_at=%s, updated_at=%s, dat=%s''',i)


i是值列表
按照文档https://docs.djangoproject.com/en/2.1/topics/db/sql/中给出的自定义sql

    /usr/local/lib/python2.7/dist-packages/django/db/backends/utils.pyc in execute(self, sql, params)
 77         start = time()
 78         try:
---> 79             return super(CursorDebugWrapper, self).execute(sql, params)
 80         finally:
 81             stop = time()

/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.pyc in execute(self, sql, params)
 62                 return self.cursor.execute(sql)
 63             else:
--> 64                 return self.cursor.execute(sql, params)
 65 
 66     def executemany(self, sql, param_list):

/usr/local/lib/python2.7/dist-packages/django/db/utils.pyc in __exit__(self, exc_type, exc_value, traceback)
 92                 if dj_exc_type not in (DataError, IntegrityError):
 93                     self.wrapper.errors_occurred = True
---> 94                 six.reraise(dj_exc_type, dj_exc_value, traceback)
 95 
 96     def __call__(self, func):

/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.pyc in execute(self, sql, params)
 62                 return self.cursor.execute(sql)
 63             else:
---> 64                 return self.cursor.execute(sql, params)
 65 
 66     def executemany(self, sql, param_list):

/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.pyc in execute(self, query, params)
335             return Database.Cursor.execute(self, query)
336         query = self.convert_query(query)    
--> 337         return Database.Cursor.execute(self, query, params)
338 
339     def executemany(self, query, param_list):

OperationalError: near "?": syntax error


我从未使用过'?' django要求使用'%s'的地方 该怎么办..谢谢

1 个答案:

答案 0 :(得分:1)

您的i变量必须是列表 我= [...] p变量是一个游标对象 p.execute('插入或替换为 webapp_information(登录名,idd,avatar_url,gravatar_id,url,html_url,followers_url,following_url,gissurl,starred_url,subscriptions_url,organizations_url,repos_url,events_url,received_events_url,typ,site_admin,名称,公司,博客,位置,电子邮件,可租用public_repos,public_gists,关注者,关注者,created_at,updated_at,dat)VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,% s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s, %s,%s,%s)',i)