简体   繁体   English

如何在Windows命令提示符下执行sql Python脚本?

[英]How to execute sql Python script in Windows command Prompt?

I have a simple script that uses sqlite3 in Python. 我有一个简单的脚本,在Python中使用sqlite3。

However, when I run this from cmd.exe in Windows I get an "Open With" window. 但是,当我从Windows中的cmd.exe运行此程序时,会出现“打开方式”窗口。 If I click 'cancel' it says "Access is denied." 如果单击“取消”,则会显示“访问被拒绝”。 in cmd.exe. 在cmd.exe中。

import sqlite3
connection = sqlite3.connect("test_database.db")
c = connection.cursor()
c.execute("CREATE TABLE People(FirstName TEXT, LastName TEXT, Age INT)")
c.execute("INSERT INTO People VALUES('Ron','Obvious',42)")
connection.commit()
connection.close()

I can run Python scripts fine from cmd.exe. 我可以从cmd.exe运行Python脚本。 I just have this problem because I'm using sqlite? 我只是有这个问题,因为我正在使用sqlite?

(Also, I have the sqlite path in PATH) (另外,我在PATH中有sqlite路径)

How can I stop this "Open with" window appearing, and actually get my script to run correctly from cmd.exe? 如何停止出现“打开方式”窗口,并从cmd.exe正确运行我的脚本?

Running myscript.py in cmd does not work if I use import sqlite. 如果我使用import sqlite,则无法在cmd中运行myscript.py。 Instead, I ran "python myscript.py" and the script runs fine. 相反,我运行了“ python myscript.py”,脚本运行正常。

Thanks to @nerdwaller. 感谢@nerdwaller。

In Windows, to execute a script that needs an interpreter, you can either run the interpreter manually: 在Windows中,要执行需要解释器的脚本,可以手动运行解释器:

C:\> python myscript.py

or ensure that .py files are associated with python.exe (if you get the "open with" window, it's likely that this was not done), then run the script with start , which is the equivalent of double-clicking: 或确保.py文件与python.exe关联(如果您获得“打开方式”窗口,则可能未完成此操作),然后使用start运行脚本,这相当于双击:

C:\> start /b/wait myscript.py

or change the PATHEXT environment variable to allow executing files with that extension directly (this behaves then the same as the above start command): 或更改PATHEXT环境变量以允许直接执行具有该扩展名的文件(其行为与上述start命令相同):

C:\> set pathext=%pathext%;.py
C:\> myscript

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM