nodejs操作mysql

demo


1. 安装

$ npm install mysql

2. 代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'root',
database : 'test'
});
connection.connect();
connection.query('select * from test', function(err, rows) {
if (err) throw err;
console.log('The solution is: ', rows);
console.log(fiels);
});
connection.end();

3. 结束

java操作mysql

demo


1. 下载java的mysql驱动

mysql-connector-java-5.1.26-bin.jar 下载

2. 导入工程

在工程的树状文件结构里右击以上jar文件选择build path再选择add to build path

3. 代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JavaMysqlConnection{
public static void main(String[] args){
//variables used to operate the mysql database
Connection connection = null;
Statement statement = null;
String sql = null;
ResultSet resultSet = null;
//the information of mysql database
String user = "root";
String password = "root";
String url = "jdbc:mysql://localhost/test";
//look up the mysql diriver , is it useful ?
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
System.out.println("mysql driver is not ready ...");
e.printStackTrace();
}
// open a mysql connection to java application
try{
connection = DriverManager.getConnection(url,user,password);//url,user,password
}catch(SQLException e){
System.out.println("there is sql exception when connecting ...");
e.printStackTrace();
}
// operate the mysql
try{
statement = connection.createStatement();
sql = "select * from test";//sql to execute
resultSet = statement.executeQuery(sql);
while(resultSet.next()){
System.out.println("id:"+resultSet.getInt("id")+"\tname:"+resultSet.getString("name")+"\tage:"+resultSet.getInt("age")+"\tdescription:"+resultSet.getString("description"));
}
}catch(SQLException e){
System.out.println("there is sql exception ...");
e.printStackTrace();
}
// close the connection
try{
if(resultSet!=null){
resultSet.close();
resultSet = null;
}
if(statement!=null){
statement.close();
statement = null;
}
if(connection!=null){
connection.close();
connection = null;
}
}catch(Exception e){
System.out.println("something was wrong when closing the connection to mysql ...");
e.printStackTrace();
}
System.out.println("java connection to mysql is ending...");
}
}

4. insert

1
2
sql = "insert into test(id, name, age, description) values(88,'sofia', 19, 'this is sofia ...')";
statement.executeUpdate(sql);

5. update

1
2
sql = "update test set name='tony' where id>6";
statement.executeUpdate(sql);

6. delete

1
2
sql = "delete from test where name='tony'";
statement.execute(sql);

7. 参考

波纹的博客

python连接mysql

demo


1. 安装PyMySQL

$ pip install pymysql

github地址

2. select操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import pymysql
connection = pymysql.connect(
host="localhost",
port = 3306,
user = "root",
password = "root",
db = "test",
charset = "utf8"
)
cur = connection.cursor()
cur.execute("select * from test")
# row = cur.fetchone()
# print (row)
res = cur.fetchall()
for row in res:
print (row)
connection.close()
val = input("press any key to exit ...")

3. insert操作

1
2
3
4
5
6
with connection.cursor() as cur:
sql = "insert into test(id, name, age, description) values(11,'tony',%s,%s)"
cur.execute(sql, (30, "this is description to tony ..."))
# connection is not autocommit by default.
# So you must commit to save your changes.
connection.commit()

4. 错误处理

1
2
3
4
5
6
7
# open the connection
# connection = pymysql.connect()
try:
# with connection.cursor() as cursor:
# do something with cursor
finally:
# close the conection

python模块tkinter的events

demo
原文

1. event binding

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from Tkinter import *
root = Tk()
def key(event):
print "pressed", repr(event.char)
def callback(event):
frame.focus_set()
print "clicked at", event.x, event.y
frame = Frame(root, width=100, height=100)
frame.bind("<Key>", key)
frame.bind("<Button-1>", callback)
frame.pack()
root.mainloop()

2. events

1
2
3
4
5
6
7
8
9
10
11
12
13
<Button-1>
<B1-Motion>
<ButtonRelease-1>
<Double-Button-1>
<Enter>
<Leave>
<FocusIn>
<FocusOut>
<Return>
<Key>
a # The user typed an “a”.
<Shift-Up> # The user pressed the Up arrow, while holding the Shift key pressed.
<Configure> # The widget changed size (or location, on some platforms)

1. event object

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# get the event object
def key(event):
print "pressed", repr(event.char)
print "clicked at", event.x, event.y
# attributes
widget
x, y
x_root, y_root
char
keysym
keycode
num
width, height
type

python模块tkinter的widget讲解

demo
原文

1. options

1
2
3
4
5
6
val = cget(“option”) => string
config(option=value, …)
configure(option=value, …)
val = elem["option"]
elem["option"] = "value"
button = Button(frame, {"text": "QUIT", "fg": "red", "command": frame.quit})

2. print the method and options

1
2
print (elem.keys())
print (dir(elem))

3. styling

1
2
rgb = widget.winfo_rgb("red")
red, green, blue = rgb[0]/256, rgb[1]/256, rgb[2]/256

3. fonts

1
2
3
4
5
6
7
8
9
10
11
("Times", 10, "bold")
("Helvetica", 10, "bold italic")
("Symbol", 8)
"Times 10 bold"
"Helvetica 10 bold italic"
"Symbol 8"
# tkFont module
tkFont.Font(family="Times", size=10, weight=tkFont.BOLD)
tkFont.Font(family="Helvetica", size=10, weight=tkFont.BOLD,
slant=tkFont.ITALIC)
tkFont.Font(family="Symbol", size=8)

4. Text Formatting

1
2
justify = # option to LEFT or RIGHT. The default value is CENTER.
wraplength = # option to set a maximum width,

1. border

1
2
3
4
5
6
borderwidth (or bd)
relief = # SUNKEN, RAISED, GROOVE, RIDGE, and FLAT.
# Focus Highlights #
highlightcolor
highlightbackground
highlightthickness

1. cursor

1
2

python-tkinter-menu

demo
原文

1. Menubutton

1
2
3
4
5
mb = tkinter.Menubutton(
root,
bg = "red"
)
mb.pack()

2.Menu

1
2
3
4
5
6
7
8
def hello():
print ("hello ...")
m = tkinter.Menu(
root
)
m.add_command(label = "one",command = hello)
m.add_command(label = "two",command = hello)
root.config(menu = m)

3. Menu

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def hello():
print ("hello!")
menubar = tkinter.Menu(root)
# create a pulldown menu, and add it to the menu bar
filemenu = tkinter.Menu(menubar, tearoff=0)
filemenu.add_command(label="Open", command=hello)
filemenu.add_command(label="Save", command=hello)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
# create more pulldown menus
editmenu = tkinter.Menu(menubar, tearoff=0)
editmenu.add_command(label="Cut", command=hello)
editmenu.add_command(label="Copy", command=hello)
editmenu.add_command(label="Paste", command=hello)
menubar.add_cascade(label="Edit", menu=editmenu)
helpmenu = tkinter.Menu(menubar, tearoff=0)
helpmenu.add_command(label="About", command=hello)
menubar.add_cascade(label="Help", menu=helpmenu)
# display the menu
root.config(menu=menubar)

4. context menu

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def hello():
print ("hello!")
# create a popup menu
menu = tkinter.Menu(root, tearoff=0)
menu.add_command(label="Undo", command=hello)
menu.add_command(label="Redo", command=hello)
# create a canvas
frame = tkinter.Frame(root, width=212, height=212)
frame.pack()
def popup(event):
menu.post(event.x_root, event.y_root)
# attach popup to canvas
frame.bind("<Button-3>", popup)

4. menu update

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
counter = 0
def update():
global counter
counter = counter + 1
menu.entryconfig(0, label=str(counter))
root = Tk()
menubar = Menu(root)
menu = Menu(menubar, tearoff=0, postcommand=update)
menu.add_command(label=str(counter))
menu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="Test", menu=menu)
root.config(menu=menubar)

5. methods

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Menu(master=None, **options)
__init__(master=None, **options) [#]
activate(index) [#]
add(type, **options) [#]
add_cascade(**options) [#]
add_checkbutton(**options) [#]
add_command(**options) [#]
add_radiobutton(**options) [#]
add_separator(**options) [#]
config(**options) [#]
delete(index1, index2=None) [#]
entrycget(index, option) [#]
entryconfig(index, **options) [#]
entryconfigure(index, **options) [#]
index(index) [#]
insert(index, itemType, **options) [#]
insert_cascade(index, **options) [#]
insert_checkbutton(index, **options) [#]
insert_command(index, **options) [#]
insert_radiobutton(index, **options) [#]
insert_separator(index, **options) [#]
invoke(index) [#]
post(x, y) [#]
type(index) [#]
unpost() [#]
yposition(index) [#]

python模块tkinter的图片运用

demo
原文

1. PhotoImage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# The PhotoImage class can read GIF and PGM/PPM images from files:
photo = PhotoImage(file="image.gif")
# base64
photo = """
R0lGODdhEAAQAIcAAAAAAAEBAQICAgMDAwQEBAUFBQYGBgcHBwgICAkJCQoKCgsLCwwMDA0NDQ4O
Dg8PDxAQEBERERISEhMTExQUFBUVFRYWFhcXFxgYGBkZGRoaGhsbGxwcHB0dHR4eHh8fHyAgICEh
...
AfjHtq1bAP/i/gPwry4AAP/yAtj77x+Af4ABAwDwrzAAAP8SA/j3DwCAfwAA/JsM4J/lfwD+/QMA
4B8AAP9Ci/4HoLTpfwD+qV4NoHVAADs=
"""
photo = PhotoImage(data=photo)
# If you need to work with other file formats, the Python Imaging Library (PIL) contains classes that lets you load images in over 30 formats, and convert them to Tkinter-compatible image objects:
from PIL import Image, ImageTk
image = Image.open("lenna.jpg")
photo = ImageTk.PhotoImage(image)

2. BitmapImage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# base64
BITMAP = """
#define im_width 32
#define im_height 32
static char im_bits[] = {
0xaf,0x6d,0xeb,0xd6,0x55,0xdb,0xb6,0x2f,
0xaf,0xaa,0x6a,0x6d,0x55,0x7b,0xd7,0x1b,
0xad,0xd6,0xb5,0xae,0xad,0x55,0x6f,0x05,
0xad,0xba,0xab,0xd6,0xaa,0xd5,0x5f,0x93,
0xad,0x76,0x7d,0x67,0x5a,0xd5,0xd7,0xa3,
0xad,0xbd,0xfe,0xea,0x5a,0xab,0x69,0xb3,
0xad,0x55,0xde,0xd8,0x2e,0x2b,0xb5,0x6a,
0x69,0x4b,0x3f,0xb4,0x9e,0x92,0xb5,0xed,
0xd5,0xca,0x9c,0xb4,0x5a,0xa1,0x2a,0x6d,
0xad,0x6c,0x5f,0xda,0x2c,0x91,0xbb,0xf6,
0xad,0xaa,0x96,0xaa,0x5a,0xca,0x9d,0xfe,
0x2c,0xa5,0x2a,0xd3,0x9a,0x8a,0x4f,0xfd,
0x2c,0x25,0x4a,0x6b,0x4d,0x45,0x9f,0xba,
0x1a,0xaa,0x7a,0xb5,0xaa,0x44,0x6b,0x5b,
0x1a,0x55,0xfd,0x5e,0x4e,0xa2,0x6b,0x59,
0x9a,0xa4,0xde,0x4a,0x4a,0xd2,0xf5,0xaa
};
"""
bitmap = BitmapImage(data=BITMAP)
bitmap = BitmapImage(
data=BITMAP,
foreground="white",
background="black",
maskdata=MASK_BITMAP
)
# file
bitmap = BitmapImage(file="bitmap.xbm")
# bitmap config
bitmap.config(foreground="blue")
bitmap["foreground"] = "red"
print bitmap["foreground"]

3. usage

1
2
3
4
5
6
7
# photo
label = Label(image=photo)
label.image = photo # keep a reference!
label.pack()
# bitmap
label = Label(image=bitmap)
label.pack()

python模块tkinter的variable

demo
原文

1. type

1
2
3
4
5
# The Variable Classes
BooleanVar
DoubleVar
IntVar
StringVar

2. usage

1
2
val = StringVar()
label = Label(text = "text...",textvariable = val)

3. methods

1
2
3
4
5
6
7
8
9
# value
value = val.get()
val.set("new text ...")
# other
# mode is one of the "w","u","r"
trace(mode, callback)
trace_variable(mode, callback)
trace_vdelete(mode, observer name)
trace_vinfo()

python模块tkinter的window-methods

demo
原文

1. args

1
2
3
4
5
6
7
8
attributes(*args) # Sets or gets window attributes.
# args
alpha=
disabled=
modified=
titlepath=
toolwindow=
topmost=

2. methods

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
client(name=None) [#]
colormapwindows(*wlist) [#]
command(value=None)
deiconify()
iconify()
iconname(newName=None) [#]
iconmask(bitmap=None) [#]
iconposition(x=None, y=None) [#]
iconwindow(window=None) [#]
geometry(geometry=None) [#]
resizable(width=None, height=None) [#]
iconbitmap(bitmap=None) [#]
maxsize(width=None, height=None) [#]
minsize(width=None, height=None) [#]
overrideredirect(flag=None) [#]
positionfrom(who=None) [#]
sizefrom(who=None) [#]
state(newstate=None) [#] iconic,withdraw,icon,normal
title(string=None) [#]
withdraw() [#] # Removes the window from the screen (without destroying it). To redraw the window, use deiconify.

python模块tkinter的komponent-methods

demo
原文

1. configure

1
2
3
w.config(option=value)
value = w.cget("option")
k = w.keys()

2. Event processing

1
2
3
4
5
6
7
8
mainloop()
w.mainloop()
w.quit()
w.wait_variable(var)
w.wait_visibility(window)
w.wait_window(window)
w.update()
w.update_idletasks()

3. Event callbacks

1
2
3
4
5
w.bind(event, callback)
w.unbind(event)
w.bind_class(event, callback)
w.bindtags()
w.bindtags(tags)

4. Alarm handlers and other non-event callbacks

1
2
3
id = w.after(time, callback)
id = w.after_idle(callback)
w.after_cancel(id)

5. Window management

1
2
w.lift()
w.lower()
1
2
3
w.winfo_width(), w.winfo_height()
w.winfo_reqwidth(), w.winfo_reqheight()
w.winfo_id()

7. The option database

1
2
w.option_add(pattern, value)
w.option_get(name, class)

8. window

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
destroy()
winfo_rooty() [#]
winfo_rootx() [#]
winfo_rgb(color) [#]
winfo_screen() [#]
winfo_screencells() [#]
winfo_screendepth() [#]
winfo_screenheight() [#]
winfo_screenmmheight() [#]
winfo_screenmmwidth() [#]
winfo_screenvisual() [#]
winfo_screenwidth() [#]
winfo_viewable() [#]
winfo_vrootheight() [#]
winfo_vrootwidth() [#]
winfo_vrootx() [#]
winfo_vrooty() [#]
winfo_width() [#]
winfo_x() [#]
winfo_y() [#]

9. clipboard

1
2
clipboard_append(string, **options) [#]
clipboard_clear(**options) [#]
,