如何从命令行中删除PostgreSQL中的所有表?
我不希望删除数据库本身,只是所有的表格,并在他们所有的数据.
如何在Python中执行以下操作?
row = [unicode(x.strip()) for x in row if x is not None else '']
Run Code Online (Sandbox Code Playgroud)
实质上:
SQLAlchemy flush()和commit()SQLAlchemy 之间有什么区别?
我已经阅读了文档,但没有更明智 - 他们似乎假设我没有预先理解.
我对它们对内存使用的影响特别感兴趣.我正在从一系列文件(总共约500万行)中将一些数据加载到数据库中,并且我的会话偶尔会崩溃 - 它是一个大型数据库和一台内存不足的机器.
我想知道我是否使用了太多但commit()没有足够的flush()电话 - 但是如果没有真正了解其中的差异,那就很难说了!
如何descending在SQLAlchemy查询中使用ORDER BY ,如下所示?
此查询有效,但按升序返回:
query = (model.Session.query(model.Entry)
.join(model.ClassificationItem)
.join(model.EnumerationValue)
.filter_by(id=c.row.id)
.order_by(model.Entry.amount) # This row :)
)
Run Code Online (Sandbox Code Playgroud)
如果我尝试:
.order_by(desc(model.Entry.amount))
Run Code Online (Sandbox Code Playgroud)
然后我得到:NameError: global name 'desc' is not defined.
我正在使用Python写入postgres数据库:
sql_string = "INSERT INTO hundred (name,name_slug,status) VALUES ("
sql_string += hundred + ", '" + hundred_slug + "', " + status + ");"
cursor.execute(sql_string)
Run Code Online (Sandbox Code Playgroud)
但由于我的一些行是相同的,我收到以下错误:
psycopg2.IntegrityError: duplicate key value
violates unique constraint "hundred_pkey"
Run Code Online (Sandbox Code Playgroud)
如何编写'INSERT除非此行已存在'SQL语句?
我见过这样的复杂陈述:
IF EXISTS (SELECT * FROM invoices WHERE invoiceid = '12345')
UPDATE invoices SET billed = 'TRUE' WHERE invoiceid = '12345'
ELSE
INSERT INTO invoices (invoiceid, billed) VALUES ('12345', 'TRUE')
END IF
Run Code Online (Sandbox Code Playgroud)
但首先,这对于我需要的东西是否过度,其次,我怎样才能将其中一个作为简单的字符串执行?
我可以在CSS参考中看到如何设置图像透明度以及如何设置背景图像.但是,如何组合这两个以设置透明背景图像?
我有一个我想用作背景的图像,但它太亮了 - 我想将不透明度降低到约0.2.我怎样才能做到这一点?
#main {
background-image: url(/wp-content/uploads/2010/11/tandem.jpg);
}
Run Code Online (Sandbox Code Playgroud) 我认为'解包'可能是错误的词汇 - 道歉,因为我确信这是一个重复的问题.
我的问题很简单:在一个需要项目列表的函数中,如何在不出错的情况下传递Python列表项?
my_list = ['red', 'blue', 'orange']
function_that_needs_strings('red', 'blue', 'orange') # works!
function_that_needs_strings(my_list) # breaks!
Run Code Online (Sandbox Code Playgroud)
当然必须有一种方法来扩展列表,并'red','blue','orange'在蹄上传递函数?
如果我在Django中设置会话变量,例如:
request.session["name"] = "name"
Run Code Online (Sandbox Code Playgroud)
有没有办法可以在模板中访问它,或者我是否必须从视图中检索它,然后将其传递给模板?
问,因为我有大约10个小会话变量,我想在模板中访问,并将所有10个从视图传递到模板可能会有点乱.
(我必须使用会话变量,因为它是一个HttpResponseRedirect,但是为了我的目的,将变量存储在数据库中是过度的.)
那么 - 任何直接在模板中获取会话变量的方法?
我有一个Python字典列表,如下所示:
a = [
{'main_color': 'red', 'second_color':'blue'},
{'main_color': 'yellow', 'second_color':'green'},
{'main_color': 'yellow', 'second_color':'blue'},
]
Run Code Online (Sandbox Code Playgroud)
我想检查列表中是否已存在具有特定键/值的字典,如下所示:
// is a dict with 'main_color'='red' in the list already?
// if not: add item
Run Code Online (Sandbox Code Playgroud) 我正在使用一些CSV文件,使用以下代码:
reader = csv.reader(open(filepath, "rU"))
try:
for row in reader:
print 'Row read successfully!', row
except csv.Error, e:
sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))
Run Code Online (Sandbox Code Playgroud)
一个文件抛出此错误:
file my.csv, line 1: line contains NULL byte
Run Code Online (Sandbox Code Playgroud)
我能做什么?谷歌似乎暗示它可能是一个Excel文件被不正当地保存为.csv.有什么方法可以解决Python中的这个问题吗?
==更新==
按照下面@ JohnMachin的评论,我尝试将这些行添加到我的脚本中:
print repr(open(filepath, 'rb').read(200)) # dump 1st 200 bytes of file
data = open(filepath, 'rb').read()
print data.find('\x00')
print data.count('\x00')
Run Code Online (Sandbox Code Playgroud)
这是我得到的输出:
'\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1\x00\x00\x00\x00\x00\x00\x00\x00\ .... <snip>
8
13834
Run Code Online (Sandbox Code Playgroud)
所以该文件确实包含NUL字节.
python ×7
list ×2
postgresql ×2
sqlalchemy ×2
css ×1
csv ×1
dictionary ×1
django ×1
sql-insert ×1