我使用psycopg2连接到Python上的PostgreSQL,我想使用连接池.
当我执行INSERT查询时,我不知道该怎么做而不是commit()和rollback().
db = pool.SimpleConnectionPool(1, 10,host=conf_hostname,database=conf_dbname,user=conf_dbuser,password=conf_dbpass,port=conf_dbport)
# Get Cursor
@contextmanager
def get_cursor():
con = db.getconn()
try:
yield con.cursor()
finally:
db.putconn(con)
with get_cursor() as cursor:
cursor.execute("INSERT INTO table (fields) VALUES (values) RETURNING id")
id = cursor.fetchone()
Run Code Online (Sandbox Code Playgroud)
没有commit()我没有得到插入记录的id.
我想.deb
为我的 python 程序创建包。我创建setup.py
和它工作得很好,但是当我想用创建一个Debian软件包setup.py
和stdeb
我得到一个错误。
我用来创建 .deb 的命令:
第一的:
python3.4 setup.py sdist
Run Code Online (Sandbox Code Playgroud)
然后:
python3.4 setup.py --command-packages=stdeb.command bdist_deb
Run Code Online (Sandbox Code Playgroud)
但得到错误
dpkg-buildpackage: warning: build dependencies/conflicts unsatisfied; aborting
dpkg-buildpackage: warning: (Use -d flag to override.)
Traceback (most recent call last):
File "setup.py", line 19, in <module>
install_requires=['setproctitle', 'psycopg2', 'psutil']
File "/usr/lib/python3.4/distutils/core.py", line 148, in setup
dist.run_commands()
File "/usr/lib/python3.4/distutils/dist.py", line 955, in run_commands
self.run_command(cmd)
File "/usr/lib/python3.4/distutils/dist.py", line 974, in run_command
cmd_obj.run()
File "/usr/local/lib/python3.4/dist-packages/stdeb/command/bdist_deb.py", line 48, in run
util.process_command(syscmd,cwd=target_dirs[0])
File …
Run Code Online (Sandbox Code Playgroud) 连接在这里
class connection{
private $hostname = "localhost";
private $username = "root";
private $password = "";
private $database = "idea";
private $conn;
public function __construct(){
$this->conn = new mysqli($this->hostname, $this->username, $this->password, $this->database)or die("Error Connection To MySQL");
}
public function getConn(){
return $this->conn;
}
?>
Run Code Online (Sandbox Code Playgroud)
我怀疑它的连接,但只是因为...它一直在为所有其他查询工作,但谁知道.
其次,包括都在这里
<?php
session_start();
if ($_SESSION['loggedin'] != 1) {
header('location: index.php');
}
include 'connection.php';
include 'users.php';
include 'ideas.php';
$conn = new connection();
$user = new users($conn->getConn());
$idea = new ideas($conn->getConn());
?>
Run Code Online (Sandbox Code Playgroud)
倒数第二是我在类中的查询
<?php
class ideas{
private $conn; …
Run Code Online (Sandbox Code Playgroud) 我有一个带有时间戳字段和使用字段的表,如下所示
timestamp | usage
'2015-06-13 13:45:58' | 240
'2015-06-13 13:45:59' | 480
'2015-06-13 13:46:00' | 240
'2015-06-13 13:46:01' | 320
...
Run Code Online (Sandbox Code Playgroud)
我想以“30 分钟”的间隔获得“1 周”的使用总和。我只能获取以分钟、小时、天为单位的数据......
SELECT date_trunc('minute', timestamp) as clock, sum(usage)
FROM my_table
WHERE timestamp > localtimestamp - INTERVAL '1 week'
GROUP BY clock
Run Code Online (Sandbox Code Playgroud)
如何获取诸如“5 分钟”、“30 分钟”、“2 天”和...等间隔的数据。
我使用PostgreSQL创建我的数据库并保存我的用户列表,当我尝试通过java jdbc连接数据库时,我收到错误说:
"java.sql.SQLException:无效的数据库地址:jdbc:postgresql:// localhost:5432/users".
我使用PostgreSQL网站上的"JDBC41 Postgresql Driver,Version 9.3-1102".这是我的代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class javaconnect {
private static Connection c = null;
public static Connection connectDb() {
try {
Class.forName("org.postgresql.Driver");
c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/users", "postgres", "12345");
return c;
} catch (ClassNotFoundException | SQLException e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
return null;
}
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
我有一个包含28列和7M条记录的表,没有主键。
CREATE TABLE records (
direction smallint,
exporters_id integer,
time_stamp integer
...
)
Run Code Online (Sandbox Code Playgroud)
我在此表和真空表上创建索引,之后(自动真空开启)
CREATE INDEX exporter_dir_time_only_index ON sacopre_records
USING btree (exporters_id, direction, time_stamp);
Run Code Online (Sandbox Code Playgroud)
我想执行此查询
SELECT count(exporters_id) FROM records WHERE exporters_id = 50
Run Code Online (Sandbox Code Playgroud)
该表具有6982224记录,且exporters_id =50。我希望此查询使用仅索引扫描来获取结果,但使用顺序扫描。这是“ EXPLAIN ANALYZE”输出:
Aggregate (cost=204562.25..204562.26 rows=1 width=4) (actual time=1521.862..1521.862 rows=1 loops=1)
-> Seq Scan on sacopre_records (cost=0.00..187106.88 rows=6982149 width=4) (actual time=0.885..1216.211 rows=6982224 loops=1)
Filter: (exporters_id = 50)
Rows Removed by Filter: 2663
Total runtime: 1521.886 ms
Run Code Online (Sandbox Code Playgroud)
但是当我将exporters_id更改为另一个ID时,查询使用仅索引扫描
Aggregate (cost=46.05..46.06 rows=1 width=4) (actual time=0.321..0.321 rows=1 loops=1)
-> …
Run Code Online (Sandbox Code Playgroud) 我在多线程 python 程序中使用 psycopg2 (2.6) 连接到 PostgreSQL 数据库。
当程序中的队列大小增加时,选择查询会得到错误“没有要获取的结果”,但将记录插入数据库效果很好。
示例代码:
class Decoders(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
self.decode()
def decode(self):
queue = self.queue
db = Database()
while queue.qsize() > 0:
# calling db methods, just an example
temp = queue.get()
db.select_records()
db.insert_record(temp)
Run Code Online (Sandbox Code Playgroud)
和:
Decoders(queue).start()
Decoders(queue).start()
Run Code Online (Sandbox Code Playgroud)
注意:我在多处理方面没有这个问题。
编辑:
当我只启动一个线程时,程序没有任何问题。
数据库类:
class Database:
db = object
cursor = object
def __init__(self):
self.db = connect(host=conf_hostname,
database=conf_dbname,
user=conf_dbuser,
password=conf_dbpass,
port=conf_dbport)
self.db.autocommit = True
self.cursor = self.db.cursor()
def select_records(self):
self.cursor.execute(simple select) …
Run Code Online (Sandbox Code Playgroud) 我有一个包含 IP 地址及其网络掩码的表,现在我想从表中按网络地址选择位于同一网络中的 IP。
ip_adress | netmask
----------------|-----------
192.168.13.25 | 29
192.168.13.26 | 29
192.168.13.1 | 30
192.168.13.2 | 30
Run Code Online (Sandbox Code Playgroud)
例如,我想要表中网络地址 192.168.13.24/29 中的 IP:
ip_adress | netmask
----------------|-----------
192.168.13.25 | 29
192.168.13.26 | 29
Run Code Online (Sandbox Code Playgroud) 我有2张这样的桌子:
Table A Table B
+----------------+ +----------------+
| id | usage-a | | id | usage-b |
|----------------| |----------------|
| 1 | v1 | | 3 | v5 |
| 2 | v2 | | 4 | v6 |
| 3 | v3 | | 5 | v7 |
| 4 | v4 | | 6 | v8 |
+----------------+ +----------------+
Run Code Online (Sandbox Code Playgroud)
我想要这个:
Table A
+--------------------------+
| id | usage-a | usage-b |
|--------------------------|
| 1 | v1 | null | …
Run Code Online (Sandbox Code Playgroud)