Tag: database

C#

Add new column to database table programmatically

Let’s see how we can add a new column to an existing table in database using c#. SqlConnection Connection = null;   try { Connection = new SqlConnection( "Data Source=" + sql_server_ip_or_hostname + ";" + "Initial Catalog=" + database_name + ";" + "User Id=" + username + ";" + "Password=" + password + ";" + …

Microsoft SQL Server

Get Field Name and Type of Database Table

Today we will see how we can retrieve table’s columns’ names and types. USE mydatabase GO   SELECT column_name, data_type FROM information_schema.columns WHERE TABLE_NAME = ‘mytable’;use mydatabase go SELECT column_name, data_type FROM information_schema.columns WHERE table_name = ‘mytable’;

MySQL, Python

Connect to a MySQL database in Python

import MySQLdb as mysqlib import sys   dbserver = "localhost" dbuser = "username" dbpass = "********" db = "dbname" conn = None   try: conn = mysqlib.connect(dbserver, dbuser, dbpass, db); print "Connected!n"   cursor = conn.cursor() cursor.execute("SELECT * from datatable")   results = cursor.fetchall()   for record in results: print record[0] , ", ", record[1] …

MySQL

Calculate MySQL Database size

Today we will see how we can retrieve MySQL database size. All databases: SELECT table_schema "Database", SUM( data_length + index_length) / 1024 / 1024 "size in MB" FROM information_schema.TABLES GROUP BY table_schema ;SELECT table_schema "Database", SUM( data_length + index_length) / 1024 / 1024 "size in MB" FROM information_schema.TABLES GROUP BY table_schema ; Specific database: SELECT …