terminal chat program

For all coding issues - MODers and programmers, HTML and more.

Moderators: Jeff250, fliptw

Post Reply
User avatar
Isaac
DBB Artist
DBB Artist
Posts: 7127
Joined: Mon Aug 01, 2005 8:47 am
Location: Ơ̸̦͇̲̬̭̱̰͎̞͈̣͎͚̳ͬ͋̃̀̇͊͂͋͐ͦ̽ͣ̂ͥ͊̅̀̚͠ B̶͖̯͉̜̰̲̓̔͋̈́ͅ È̯ Y̪̤̼͉̠̙͝

terminal chat program

Post by Isaac » Fri Sep 16, 2011 8:20 am

I will do the challenges after this. I really had to get a good introduction to MySQL going first.

Check it out. I made a terminal chat program. It's kinda silly, because its a terminal based chat program, that only updates when you make a post. Kinda weird. But it works. Saves to my msql table and everything.

What do you all think?

Code: Select all

import MySQLdb


class chatsys:
	def __init__(self):
		self.newcomment=""
		chat = MySQLdb.connect( host = "localhost" , user="isaac" , passwd="password" , db="chatbox" )
		self.cur = chat.cursor()
	def login(self):
		self.username=raw_input("User name?")
	def chat(self):
		self.comment=raw_input("Comment: (Type 'exit' to exit) ")
		self.newcomment="insert into chatlog values(NULL,'%s',NULL,'%s')" % (self.username,self.comment)
		self.cur.execute(self.newcomment)
	def showlog(self):
		self.cur.execute("select * from chatlog ORDER by -id limit 15")		
		lastfive=[a for a in self.cur.fetchall()]
		lastfive.reverse()
		for a in lastfive:
			print str(a[0])+":  "+str(a[1])+" Said: "+str(a[3])
		
c=chatsys()
c.login()

while True:
	
	c.showlog()
	c.chat()
	if c.comment == "exit":
		print "exited"
		break



c.cur.close()
edit:
Oops. Forgot to show my table description:

Code: Select all


mysql> describe chatlog;
+----------+--------------+------+-----+-------------------+-----------------------------+
| Field    | Type         | Null | Key | Default           | Extra                       |
+----------+--------------+------+-----+-------------------+-----------------------------+
| id       | mediumint(9) | NO   | PRI | NULL              | auto_increment              |
| USERNAME | char(30)     | YES  |     | NULL              |                             |
| TIMELOG  | timestamp    | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| comment  | varchar(300) | YES  |     | NULL              |                             |
+----------+--------------+------+-----+-------------------+-----------------------------+
s☼-£♦и̫͍ͥ̍ͪ͌̓͗͡о̡̹̱͊̅ͮ̓̕͢б̧̝̻̪̤̳̜͐̓̉ͤ͢͜ ͙̬͙̆̑ͮ̐ͭ̾̂́͘i̎̌̾̓̽̀̈̓̀҉͉̙̦͎̘̝͕f̻͕͔̘ͣͣ̓͊̿͢͜ ͍͔͈͕̮̫ͣ̆ͮ̊͋/♂6Æ!♪╩"▲L└уͭ̂͐̇҉̴̣̼̞̠̯͓̺̞ф̜̊͌̈́̋̏̐́ц̨͔̮̿̇ ̨̛͖̙͖̖̮̗̱ͩ̆͞ͅа̥͇̞̖͚̟̅͐ͤ͞͠͠э̜̘̩̳̬͔̾ͯ̀ͫ̒̐̿ͅͅг̭̖̀ͦ̒̑ͥ̌ͮͫ͞ё͔̟̃ͬ̾̓͟ё̦̞̙̫͔̩͑̀͂ͯ̄̔̃̑̀͠ͅͅ
User avatar
fliptw
DBB DemiGod
DBB DemiGod
Posts: 6415
Joined: Sat Oct 24, 1998 2:01 am
Location: Calgary Alberta Canada

Re: terminal chat program

Post by fliptw » Fri Sep 16, 2011 10:01 am

terminal chat programs are pretty old software.
User avatar
Jeff250
DBB Master
DBB Master
Posts: 6387
Joined: Sun Sep 05, 1999 2:01 am
Location: ☃☃☃

Re: terminal chat program

Post by Jeff250 » Fri Sep 16, 2011 12:43 pm

Does this work when the user attempts to chat:

Code: Select all

foo'bar
?
Hint: prepared statements.
User avatar
Isaac
DBB Artist
DBB Artist
Posts: 7127
Joined: Mon Aug 01, 2005 8:47 am
Location: Ơ̸̦͇̲̬̭̱̰͎̞͈̣͎͚̳ͬ͋̃̀̇͊͂͋͐ͦ̽ͣ̂ͥ͊̅̀̚͠ B̶͖̯͉̜̰̲̓̔͋̈́ͅ È̯ Y̪̤̼͉̠̙͝

Re: terminal chat program

Post by Isaac » Fri Sep 16, 2011 2:30 pm

Jeff250 wrote:Does this work when the user attempts to chat:

Code: Select all

foo'bar
?
Hint: prepared statements.
Omg. hahah wow. Nope! It does not. Thanks.
s☼-£♦и̫͍ͥ̍ͪ͌̓͗͡о̡̹̱͊̅ͮ̓̕͢б̧̝̻̪̤̳̜͐̓̉ͤ͢͜ ͙̬͙̆̑ͮ̐ͭ̾̂́͘i̎̌̾̓̽̀̈̓̀҉͉̙̦͎̘̝͕f̻͕͔̘ͣͣ̓͊̿͢͜ ͍͔͈͕̮̫ͣ̆ͮ̊͋/♂6Æ!♪╩"▲L└уͭ̂͐̇҉̴̣̼̞̠̯͓̺̞ф̜̊͌̈́̋̏̐́ц̨͔̮̿̇ ̨̛͖̙͖̖̮̗̱ͩ̆͞ͅа̥͇̞̖͚̟̅͐ͤ͞͠͠э̜̘̩̳̬͔̾ͯ̀ͫ̒̐̿ͅͅг̭̖̀ͦ̒̑ͥ̌ͮͫ͞ё͔̟̃ͬ̾̓͟ё̦̞̙̫͔̩͑̀͂ͯ̄̔̃̑̀͠ͅͅ
Post Reply