我尝试在动态SQL中调用substring函数。
from psycopg2 import sql as ps_sql
sql = ps_sql.SQL('select {}').format(ps_sql.Identifier("substring('TEST',1,2)"))
with db.conn_ogis.cursor() as cursor:
cursor.execute(sql)
我有错误: psycopg2.errors.UndefinedColumn: error:列"substring('TEST
我收到此错误消息 UndefinedColumn: column "datetime" of relation "daily_price" does not exist
LINE 1: INSERT INTO public.daily_price (DateTime) VALUES ('200...
^ 代码 cur.execute("INSERT INTO public.daily_price (DateTime) VALUES ('"
我在Python中与库psycopg2一起使用postgres。连接到数据库后,我将尝试检查具有给定名称的表是否存在。在postgres,我是这样做的:
\connect myDB
select exists(select * from pg_tables where schemaname='public' AND tablename='mytable';)
如果该表存在,但如果不存在,则可以这样做。在python中,我使用以下行进行操作:
import psycopg2 as pg
from psycopg2.extensions import ISOLATI
我试图在Rails 4应用程序中创建一些简单的关系。我有三种模式:用户,列表,单词。
这是一个简单的“列表”应用程序,其中每个用户的has_many单词通过列表。
我现在有以下的协会..。
用户模型
class User < ActiveRecord::Base
has_many :lists
has_many :words, through: :lists
end
列表模型
class List < ActiveRecord::Base
has_many :words
belongs_to :user
end
Word模型
class Word < Acti
SELECT DATENAME(month,created) AS bulan,
COUNT(created) AS jumlah
FROM ost_ticket
GROUP BY bulan
我希望从具有DATETIME类型的列中获取月份名称。这是来自创建2014-02-04 09:55:22的列的示例数据,但是我得到了错误函数datename不存在。
我正在使用python脚本将tweet流到postgres数据库(使用psycopg2)。我希望能够在windows任务管理器中安排这个脚本。我需要克服的唯一问题是能够在postgres中重命名该表。有可能吗?
x = datetime.date.today() - datetime.timedelta(days=1)
con = psycopg2.connect("dbname='test' user='postgres'")
cur = con.cursor()
cur.execute("DROP TABLE IF EXIST
如果临时表不存在,则运行创建临时表的查询时,如何确定是否真的创建了新表?
例如,如果我用CREATE TEMP TABLE IF NOT EXISTS name创建了一个临时表,那么,如果创建了一个新表,我能否让查询返回一些东西?
在我的特定情况下,在创建临时表时,我运行另一个查询将一些数据复制到其中,因此我需要确保仅在重新创建临时表的情况下才复制数据(例如,如果该表由于某些瞬时连接中断或其他原因而被删除)。我正在使用psycopg2。
我正在尝试将几个panda数据帧加载到postgresql数据库中。它们的列数略有不同,这会给出一个错误。您知道解决方法吗? 请看我的代码: import geopandas as gp
from sqlalchemy import *
from geoalchemy2 import Geometry, WKTElement
import psycopg2
for tablename in fileslist:
gdf1=gp.read_file("name.geojson")
gdf1['geom'] = gdf1['geometr
我希望使用package (2.7)列出表的所有列名。但我无法执行以下查询-
cur.execute("\d my_table");
psycopg2.ProgrammingError: syntax error at or near "\"
对于如何使用psycopg2列出表的列名,是否有替代方法?请指出任何副本。谢谢!
我正在尝试创建一个允许我只指定一个表的过程,该过程将自动向该表中添加两列。
create proc addAuditFeild (@table_name nvarchar(50))
as
if OBJECT_ID (@table_name, ''U'') is not null
begin
alter table @table_name
add moduser varchar(50),
moddate datetime
end
我做错了什么?我试过用一个主管,但还是没成功。我的exe
我试图在cursor.execute参数上运行python函数,但它只是抛出了这个错误。我用的是psycopg2
Traceback (most recent call last):
File "cliente.py", line 55, in <module>
cursorDB.execute(get_datos_animal('falsa'))
psycopg2.errors.UndefinedColumn: column "falsa" does not exist
LINE 1: ...e, clasificacion FRO
我有一个基类Place和多个使用STI约定的子类。我有一个单独的模型Post,belongs_to是Place的子类之一。
class Place < ApplicationRecord
end
class SubPlace < Place
has_many :posts, class_name: "SubPlace", foreign_key: "sub_place_id"
end
class Post < ApplicationRecord
belongs_to :sub_place, class_name: "SubP
尝试使用psycopg2从PostgreSQL数据库中提取表和列,并尝试了以下代码: def execute_sql(sql):
"""
Execute a query and commits
:param sql:
:return:
"""
conn_info = get_connection_info()
print(conn_info)
print('this is conn info above')
with psyc
我有一个postgres数据库,它的扩展是timescaledb。
我的主要索引是时间戳,我想选择最近的行。
如果我碰巧知道最近的行是在一段时间后发生的,那么我可以使用如下查询:
query = 'select * from prices where time > %(dt)s'
在这里,我指定了一个日期时间,并使用psycopg2执行查询:
# 2018-01-10 11:15:00
dt = datetime.datetime(2018,1,10,11,15,0)
with psycopg2.connect(**params) as conn:
cur =
create table Train_Types
(
id int not null primary key,
name varchar(50) not null
)
create table Trains
(
id int not null primary key,
name varchar(50) not null,
trainTypeId int not null foreign key references Train_Types(id)
)
create table Stations
(
id int not null primary key,
name varchar(50)