<p>我正在尝试使用pyodbc将datetime值插入MS SQL Server表。
如果我手动操作,比如:</p>
<pre><code>cursor.execute("""insert into currentvalue(value1,currentdatetime)
values(55,'2014-06-27 16:42:48.533')""")
</code></pre>
<p>我一点问题都没有,但是当我试着做的时候:</p>
<pre><code>currenttime = str(datetime.datetime.now())
cursor.execute("""insert into currentvalue(value1,currentdatetime)
values(55,"""+ currenttime+")")
</code></pre>
<p>我得到这个错误:</p>
<blockquote>
<p>SQL server Incorrect syntax near '07' which i think is the number after the date and starting the time.</p>
</blockquote>
<p>我也试过:</p>
<pre><code>currenttime = "'"+str(datetime.datetime.now())+"'"
</code></pre>
<p>现在这个错误出现了:</p>
<blockquote>
<p>Conversion failed when converting date and/or time from character string.</p>
</blockquote>