HW7_sample

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'test.ui'
#
# Created: Thu Jul 18 16:17:56 2013
#      by: PyQt4 UI code generator 4.10.1
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        self.oper = ''
        self.num1=0
        self.num2=0
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(357, 253)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.lineEdit = QtGui.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(20, 20, 321, 41))
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(20, 100, 101, 41))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Agency FB"))
        font.setPointSize(14)
        self.pushButton.setFont(font)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(20, 150, 101, 41))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Agency FB"))
        font.setPointSize(14)
        self.pushButton_2.setFont(font)
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.pushButton_3 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QtCore.QRect(130, 100, 101, 41))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Agency FB"))
        font.setPointSize(14)
        self.pushButton_3.setFont(font)
        self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
        self.pushButton_4 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_4.setGeometry(QtCore.QRect(130, 150, 101, 41))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Agency FB"))
        font.setPointSize(14)
        self.pushButton_4.setFont(font)
        self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
        self.pushButton_5 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_5.setGeometry(QtCore.QRect(240, 100, 101, 41))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Agency FB"))
        font.setPointSize(14)
        self.pushButton_5.setFont(font)
        self.pushButton_5.setObjectName(_fromUtf8("pushButton_5"))
        self.pushButton_6 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_6.setGeometry(QtCore.QRect(240, 150, 101, 41))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Agency FB"))
        font.setPointSize(14)
        self.pushButton_6.setFont(font)
        self.pushButton_6.setObjectName(_fromUtf8("pushButton_6"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 357, 22))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.btn_add)
        QtCore.QObject.connect(self.pushButton_6, QtCore.SIGNAL("clicked()"), self.btn_equal)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.pushButton.setText(_translate("MainWindow", "+", None))
        self.pushButton_2.setText(_translate("MainWindow", "-", None))
        self.pushButton_3.setText(_translate("MainWindow", "*", None))
        self.pushButton_4.setText(_translate("MainWindow", "/", None))
        self.pushButton_5.setText(_translate("MainWindow", "C", None))
        self.pushButton_6.setText(_translate("MainWindow", "=", None))

    def btn_add(self):
        if self.num1 == 0:
            self.num1 = int(self.lineEdit.text())
        else:
            self.num1 = self.num1 + int(self.lineEdit.text())
            print(self.num1)
    def btn_equal(self):
        print(self.num1 + int(self.lineEdit.text()))

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

ASP.NET 連接 SQLite 範例

            //建立資料表
            SQLiteConnection conn = new SQLiteConnection("Data source=mydb.sqlite");
            // Open
            conn.Open();
            //要下任何命令先取得該連結的執行命令物件
            SQLiteCommand cmd = conn.CreateCommand();
            // 建立資料表
            string sql = "CREATE TABLE IF NOT EXISTS 'address_book' (sid INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , name VARCHAR, phone VARCHAR, address VARCHAR,creation_date DATETIME DEFAULT CURRENT_TIMESTAMP)";
            cmd.Connection = conn;
            cmd.CommandText = sql;
            cmd.ExecuteNonQuery();

            cmd.CommandText = "INSERT INTO 'address_book' ('name', 'phone', 'address' ) VALUES('" + TextBox1.Text +"', '" + TextBox2.Text + "', '" + TextBox3.Text + "')";
            cmd.ExecuteNonQuery();

            // 查詢資料表
            cmd.CommandText = "SELECT * FROM address_book";
            // 執行查詢塞入 sqlite_datareader
            SQLiteDataReader sqlite_datareader = cmd.ExecuteReader();
            GridView1.DataSource = sqlite_datareader;
            GridView1.DataBind();

            conn.Close();

php+mysql 連接範例 add_mysql.php

<form action="" method="post" name="form1"><br>
Input address book data:<br>
name:<input name="name" type="text" value=""><br>
phone:<input name="phone" type="text" value=""><br>
address:<input name="address" type="text" value=""><br>
<input name="submit" type="submit" value="add"><br>
<?php

mysql_connect('localhost','root','student') or die("connect error!");
mysql_select_db('phone') or die("db error");
mysql_query("SET NAMES 'utf8'");
mysql_query("CREATE TABLE  `address_book` (
 `sid` INT( 5 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
 `name` VARCHAR( 20 ) NOT NULL ,
 `phone` VARCHAR( 20 ) NOT NULL ,
 `address` VARCHAR( 20 ) NOT NULL
) ENGINE = MYISAM ;");


if(count($_POST)>0) {
// 準備更新資料的語法
$name = $_POST["name"];
$phone = $_POST["phone"];
$address = $_POST["address"];
mysql_query("INSERT INTO address_book (name, phone, address )
		VALUES( '$name', '$phone', '$address')");
}

$result = mysql_query("SELECT * FROM address_book");
makeTable($result);

function makeTable($result)
{
echo "<table border='1'>";
//設定Title
echo "<tr><td>sid</td>";
echo "<td>name</td>";
echo "<td>phone</td>";
echo "<td>address</td>";
echo "<td></td>";
echo "<td></td></tr>";
//讀取內容
while($row = mysql_fetch_assoc($result)) {
echo "<tr><td>$row[sid]</td>";
echo "<td>$row[name]</td>";
echo "<td>$row[phone]</td>";
echo "<td>$row[address]</td>";
echo "<td><a href=add_mysql.php?op=del&sid=$row[sid]>del</a></td>";
echo "<td><a href=update.php?sid=$row[sid]&name=$row[name]&phone=$row[phone]&address=$row[address]>update</a></td></tr>";
}
echo "</table>";
}
?>

Sorted 範例

dic = {'Tom': 90, 'Mary':56, 'Bob':77, 'Alan': 80, 'Sunny':100}
lis = [90,56,77,80,100]
students = [('Tom', 90), ('Mary', 56), ('Bob', 77), ('Alan', 80), ('Sunny', 100)]

def temp_function(list):
    print(list)
    return list[0]

'''
print(dic)
print(dic.items())
print(dic.keys())
print(dic.values())
'''
print('dic sorted:')
print(sorted(dic.items(), key = temp_function))

print('lis sorted:')
print(sorted(lis))

print('students sorted:')
print(sorted(students, key= temp_function))

#lambda
print('lambda')
print(sorted(dic.items(), key = lambda x : x[1]))