def createTable(self, screen): d = dialogSQL.createOrAlterTableSQLDialog() new_tbl = d.createTable(screen) if not new_tbl: Popup("Cancelling table creation").anyKey() else: cmdStr = "CREATE TABLE " + new_tbl.tbl_name + "(" #Now add each column with its constraints colStr = "" forKeyStr = "" first = True for c in new_tbl.col_array: if (first == False): colStr = colStr + ", " + c.name + " " + c.dataType else: colStr = colStr + c.name + " " + c.dataType if (c.isUnique == True): colStr = colStr + " UNIQUE" if (c.isAutoIncrement == True): colStr = colStr + " AUTO_INCREMENT" if (c.isNotNull == True): colStr = colStr + " NOT NULL" for f in c.foreignKeys: forKeyStr == ", FOREIGN KEY (" + c.name + ") " + f first = False cmdStr = cmdStr + colStr #Now add Primary Key Constraint if (new_tbl.primaryKey != ""): cmdStr = cmdStr + ", PRIMARY KEY (" + new_tbl.primaryKey + ")" #Now add Foreign Key if (forKeyStr != ""): cmdStr = cmdStr + forKeyStr cmdStr = cmdStr + ")" if ( new_tbl.engine.upper() == "I" ): cmdStr = cmdStr + " ENGINE=InnoDB" cmdStr = cmdStr + ";" # Now let's test and look at this command string for debugging # screen.base.clear() # screen.base.border(0) # screen.base.addstr(4, 2, cmdStr) # screen.base.refresh() # screen.base.getch() cur = self.con.cursor() try: cur.execute(cmdStr) except mdb.Error, e: screen.rebuildScreen("Results") screen.base.addstr(10,10,"Query not OK. Error number is : " + str(e.args[0])) screen.base.addstr(11,10, e.args[1]) screen.base.addstr(14,10,"Press any key to continue") else:
def alterTable(self, screen): curses.echo() d = dialogSQL.createOrAlterTableSQLDialog() # dialog function actually returns full statement stmts = d.alterTable(screen) # Create command string and execute command cur = self.con.cursor() for s in stmts: screen.base.addstr(10, 10, s) screen.base.getch() for s in stmts: try: cur.execute(s) except mdb.Error, e: screen.rebuildScreen("Results") screen.base.addstr(10,10,"Query not OK. Error number is : " + str(e.args[0])) screen.base.addstr(11,10, e.args[1]) screen.base.addstr(14,10,"Press any key to continue") screen.base.getch() else: screen.rebuildScreen("Results") screen.base.addstr(10,10,"Query OK. Table created.") screen.base.addstr(14,10,"Press any key to continue") screen.base.getch()