Postgres => Mysql

http://www.internet-technologies.ru/articles/article_2190.html

https://wiki.postgresql.org/wiki/Psycopg2_Tutorial

вставка в Mysql

php.ini

[mysql]
host = localhost
database = mydb
user = root
password =
 
def insert_books(books):
    query = "INSERT INTO books(title,name) " \
            "VALUES(%s,%s)"

    try:
        dbconfig = read_db_config()
        conn = MySQLConnection(**dbconfig)

        cursor = conn.cursor()
        cursor.executemany(query, books)

        conn.commit()
    except Error as e:
        print('Error:', e)

    finally:
        cursor.close()
        conn.close()

def main():
    books = [('Harry Potter And The Order Of The Phoenix', '9780439358071'),
             ('Gone with the Wind', '9780446675536'),
             ('Pride and Prejudice (Modern Library Classics)', '9780679783268')]
    insert_books(books)

if __name__ == '__main__':
    main()
 
 
селект из Postgres

import psycopg2

try:
    conn = psycopg2.connect("dbname='mydb' user='postgres' host='localhost' password='postgres'")
except:
    print "I am unable to connect to the database"
cur = conn.cursor()
try:
    cur.execute("""SELECT * from my_schema.bar""")
except:
    print "I can't SELECT from bar"
rows = cur.fetchall()
print "\nRows: \n"for row in rows:
    print "   ", row[1]
 
 
php style
 

header('Content-type: text/html; charset=utf-8');

$dsn = "pgsql:host=localhost;port=5432;dbname=mydb;user=postgres;password=postgres";
$dbh = new PDO($dsn);

$sql = 'SELECT
  id,
  name,
  session_id
FROM
  my_schema.operator';

foreach ($dbh->query($sql) as $row) {
    $session[] = ['name' => $row['name'],'ses' => $row['session_id']];
}
echo '<pre>';
var_dump($session);

try{
    $db = new PDO("mysql:host=localhost;dbname=mydb;charset=utf8","root","");
    /*Other Codes*/
}catch(PDOException  $e ){
    echo "Error: ".$e;
}

$stmt = $db->prepare('INSERT INTO
  `operator`
(
  `session_id`,
  `operator_login`
)
VALUE (
  :session_id,
  :operator_login
  );');

foreach($session as $d) {
    echo $d['ses'];
    $stmt->execute(array(':session_id' => $d['ses'], ':operator_login' => $d['name']));
}

Популярные сообщения из этого блога

Пишем логи на C# (.NET). Легкий способ.

Средства для работы с базой данный PostgreSql

Авторизация yii 2 из базы