Google Search

Monday, March 2, 2015

Write a Python script to create a populate a customer account balance database.

Write a Python script to create a populate a customer account balance database. The database consists of records of 3 fields:

1.  Account ID

2.  Account Name

3.  Account Balance

Solution. Make sure you know the username, password and database on your MySQL server. Create the file conf.py with the following contents. Replace your server hostname, username, password and database name.

Listing 2.1. conf.py

server = "your-server-name-here" username = "your-username-here" password = "your-password-here" database = "your-databaes-here"

import MySQLdb as db import sys

import conf

conn = db.connect("shark", conf.username, conf.password, conf.database) cursor = conn.cursor()

cursor.execute("DROP TABLE IF EXISTS accounts")

# Create Tables

sql = """CREATE TABLE accounts (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50),

balance DECIMAL(10, 2))"""

cursor.execute(sql)

# Insert into Tables


sql = """INSERT INTO accounts (name, balance) VALUES ('Alice', 500.00), ('Bob', 50000.00), ('Charlie', 100.00)"""
cursor.execute(sql)

0 comments:

Post a Comment