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: 7737
Joined: Mon Aug 01, 2005 8:47 am
Location: 🍕

terminal chat program

Post by Isaac »

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              |                             |
+----------+--------------+------+-----+-------------------+-----------------------------+
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-★ ·:*¨༺꧁༺ :E ༻꧂༻¨*:·.★-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
User avatar
fliptw
DBB DemiGod
DBB DemiGod
Posts: 6459
Joined: Sat Oct 24, 1998 2:01 am
Location: Calgary Alberta Canada

Re: terminal chat program

Post by fliptw »

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

Re: terminal chat program

Post by Jeff250 »

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: 7737
Joined: Mon Aug 01, 2005 8:47 am
Location: 🍕

Re: terminal chat program

Post by Isaac »

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.
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-★ ·:*¨༺꧁༺ :E ༻꧂༻¨*:·.★-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
Post Reply