Browse Source

initial commit

master
Chris Cromer 9 months ago
commit
0bb81e5dee
Signed by: Chris Cromer <chris@cromer.cl> GPG Key ID: 39CC813FF3C8708A
7 changed files with 196 additions and 0 deletions
  1. 2
    0
      .gitignore
  2. 20
    0
      c/meson.build
  3. 40
    0
      c/postgresql_demo.c
  4. 53
    0
      java/PostgresqlDemo.java
  5. 27
    0
      php/postgresql-demo.php
  6. 30
    0
      vala/PostgresqlDemo.vala
  7. 24
    0
      vala/meson.build

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
*.jar
build

+ 20
- 0
c/meson.build View File

@@ -0,0 +1,20 @@
project('postgresqldemo',
'c',
version : '1.0.0',
license : 'BSD-3',
default_options : [ 'b_ofast=if-release', 'b_march_native=if-release', 'b_ndebug=if-release' ]
)

#gtk_dep = dependency('gtk+-3.0', version : '>=3.0.0')
# gmodule-export-2.0 is needed to connect the handlers
#gmodule_dep = dependency('gmodule-export-2.0', version : '>=2.0')
pg_dep = dependency('libpq', version : '>=8.0')

cc = meson.get_compiler('c')

sources = ['postgresql_demo.c']

exe = executable('postgresql_demo',
sources,
dependencies : [pg_dep],
install : false)

+ 40
- 0
c/postgresql_demo.c View File

@@ -0,0 +1,40 @@
#include <libpq-fe.h>

int main(int argc, char *argv[]) {
PGconn *conn;
PGresult *res;
int i;
int j;

conn = PQsetdbLogin("localhost", "5432", "", "", "database", "user", "password");
if (PQstatus(conn) != CONNECTION_OK) {
fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(conn));
PQfinish(conn);
return 1;
}

res = PQexec(conn, "SELECT * FROM turista");
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr, "SELECT failed: %s", PQerrorMessage(conn));
PQclear(res);
PQfinish(conn);
return 1;
}

for (i = 0; i < PQnfields(res); i++) {
printf("%-20s", PQfname(res, i));
}
printf("\n\n");

for (i = 0; i < PQntuples(res); i++){
for (j = 0; j < PQnfields(res); j++) {
printf("%-20s", PQgetvalue(res, i, j));
}
printf("\n");
}

PQclear(res);

PQfinish(conn);
return 0;
}

+ 53
- 0
java/PostgresqlDemo.java View File

@@ -0,0 +1,53 @@
import java.sql.*;
import java.util.Properties;

import static java.lang.System.exit;

public class PostgresqlDemo {
private Connection conn;

private PostgresqlDemo() {
String url = "jdbc:postgresql://localhost/databasename";
Properties props = new Properties();
props.setProperty("user", "bdd");
props.setProperty("password", "bdd");
try {
conn = DriverManager.getConnection(url, props);
}
catch (SQLException e) {
e.printStackTrace();
exit(1);
}
try {
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM turista");
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) {
System.out.printf("%-20s", resultSetMetaData.getColumnName(i));
}
System.out.print("\n\n");
while (resultSet.next()) {
for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) {
Object object = resultSet.getObject(i);
System.out.printf("%-20s", object == null ? "NULL" : object.toString());
}
System.out.print("\n");
}
resultSet.close();
statement.close();
}
catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
new PostgresqlDemo();
}
}

+ 27
- 0
php/postgresql-demo.php View File

@@ -0,0 +1,27 @@
<?php
$conn = pg_connect('host=localhost port=5432 dbname=bdd user=bdd password=bdd');
if (!$conn) {
echo 'Database connection failed!';
exit(1);
}
$result = pg_query($conn, 'SELECT * FROM turista');
if (!$result) {
echo 'SELECT failed: '.pg_last_error($conn);
exit(1);
}

echo '<table border=1><tr>';
for ($i = 0; $i < pg_num_fields($result); $i++) {
echo '<td>'.pg_field_name($result, $i).'</td>';
}
echo '</tr>';
while ($row = pg_fetch_row($result)) {
echo '<tr>';
for ($i = 0; $i < pg_num_fields($result); $i++) {
echo '<td>'.$row[$i].'</td>';
}
echo '</tr>';
}
echo '</table>';
pg_close($conn);
?>

+ 30
- 0
vala/PostgresqlDemo.vala View File

@@ -0,0 +1,30 @@
namespace PostgresqlDemo {
using Postgres;

public static int main (string[] args) {
Database conn = set_db_login ("localhost", "5432", "", "", "database", "user", "password");
if (conn.get_status () != ConnectionStatus.OK) {
stderr.printf ("%s\n", conn.get_error_message ());
return 1;
}

Result res = conn.exec ("SELECT * FROM turista");
if (res.get_status () != ExecStatus.TUPLES_OK) {
stderr.printf ("SELECT failed: %s", conn.get_error_message ());
return 1;
}

for (int i = 0; i < res.get_n_fields (); i++) {
stdout.printf ("%-20s", res.get_field_name (i));
}
stdout.printf ("\n\n");

for (int i = 0; i < res.get_n_tuples (); i++) {
for (int j = 0; j < res.get_n_fields (); j++) {
stdout.printf ("%-20s", res.get_value (i, j));
}
stdout.printf ("\n");
}
return 0;
}
}

+ 24
- 0
vala/meson.build View File

@@ -0,0 +1,24 @@
project('postgresqldemo',
['c', 'vala'],
version: '1.0.0',
license: 'BSD-3',
default_options: [
'b_ofast=if-release',
'b_march_native=if-release',
'b_ndebug=if-release'
]
)
add_global_arguments('-DGETTEXT_PACKAGE="sernatur"', language: 'c')

glib_dep = dependency('glib-2.0')
pq_dep = dependency('libpq', version: '>=8.0')

vala_sources = files('PostgresqlDemo.vala')

sources = vala_sources

exe = executable('postgresql_demo',
sources,
dependencies: [glib_dep, pq_dep],
install: false)


Loading…
Cancel
Save