Tuesday, December 17, 2019

Python3 connect to Microsof SQL Server

1. sudo apt-get install python3-pip
2. pip3 install pyodbc
3. sudo apt-get install unixodbc-dev
4. follow the Microsoft's documentations (ref: https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15 )
4.a
sudo su
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - 4.b curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
4.c sudo apt-get update
4.d sudo apt-get install msodbcsql17


Test connection with this code:
import pyodbcconn = pyodbc.connect("Driver={ODBC Driver 17 for SQL Server};" "Server=SERVER_IP;" "Database=DB_NAME;" "uid=DB_USERNAME;pwd=DB_PASSWORD;")conn.close()


Some error maybe occured:
> ModuleNotFoundError: No module named 'pyodbc' --> solution: pip3 install pyodbc

> pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)") --> solution: sudo apt-get install msodbcsql17

* My machine ubuntu 18.04


Monday, December 16, 2019

How to start ubuntu in command line interface

I want to set my ubuntu PC to command line interface. I don't want to start the desktop manager.

1. Edit /etc/default/grub

2. Find this section:
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
GRUB_CMDLINE_LINUX=""
3. change to:
GRUB_CMDLINE_LINUX_DEFAULT="text"
GRUB_CMDLINE_LINUX="3"

 4. run: sudo update-grub

5. Done. Every time we start the PC, it doesn't start the desktop manager automatically. If we want to start the desktop manager, run: sudo init 5


* My machine ubuntu 18.04

Monday, December 9, 2019

Can not change table structure in SQL server management studio 2008

I got this error

"Saving changes is not permitted. The changes you have made require the following tables to be dropped and re-created. You have either made changes to a table that can't be re-created or enabled the option Prevent saving changes that require the table to re-created."



when trying to change my table structure.

The solution is go to : Tools - options - designer - unchecked Prevent saving changes that require table re-creation


Monday, April 1, 2019

Python example recursive

The fibo function is using recursive. The fibo2 function using looping. Both of them will generate a Fibonacci sequence. Using recursive make our code shorter, but how about the performance?
import time

def fibo(n):
 if (n == 1 or n == 2):
  return(1)
 else:
  return(fibo(n-1)+fibo(n-2))

def fibo2(n):
 prev = 1
 curr = 1
 if (n == 1 or n == 2):
  return(1)
 elif (n < 1):
  return(-1)
 else:
  hasil = curr + prev
  for i in range(2, n):
   hasil = curr + prev
   prev = curr
   curr = hasil
  return(hasil)


iterasi = 40

start = time.time()
for i in range(iterasi):
 fibo(i+1)
end = time.time()
print("recursive " + str(end - start))

start = time.time()
for i in range(iterasi):
        fibo2(i+1)
end = time.time()
print("for " + str(end - start))


Output on my machine:

Monday, May 14, 2018

Simple snake game, c# console application


using System;
using System.Collections;
using System.Threading;


public class Snake {
 public static void Main() {
  
  ArrayList posx = new ArrayList();
  ArrayList posy = new ArrayList();
  
  int dx, dy, foodx, foody, skor, i, j
   , enemyx, enemyy, enemyTimer, batasAtas
   , delaySpeed;
  String foodChar = "f";
  String enemyChar = "e";
  
  delaySpeed = 100;

  Random r = new Random();
  
  for (i=0; i<6; i++) {
   posx.Add(35+i);
   posy.Add(13);
  }
  
  bool selesai = false;
  
  foodx = 10;
  foody = 10;
  enemyx = 70;
  enemyy = 20;
  enemyTimer = 150;
  
  dx = -1;
  dy = 0;
  skor = 0;
  batasAtas = 1;
  
  ConsoleKeyInfo key = new ConsoleKeyInfo();
  
  while(!selesai) {
   Console.Clear();
   
   Console.SetCursorPosition(1,0);
   Console.Write("Score : " + skor);

   Console.SetCursorPosition(60,0);
   Console.Write("Speed : " + (100-delaySpeed));
   
   // cetak
   for (i=0; i0; i--) {
    if ((int)posx[0] == (int)posx[i] && (int)posy[0] == (int)posy[i]) {
     // tabrak badan sendiri
     selesai = true;
    }
    posx[i] = posx[i-1];
    posy[i] = posy[i-1];
   }
   posx[0] = (int)posx[0] + dx;
   posy[0] = (int)posy[0] + dy;
   
   if ((int)posx[0] < 1) posx[0] = Console.BufferWidth-1;
   else if ((int)posx[0] > Console.BufferWidth-2) posx[0] = 1;
   
   if ((int)posy[0] < batasAtas) posy[0] = Console.BufferHeight-1;
   else if ((int)posy[0] > Console.BufferHeight-2) posy[0] = batasAtas;
   
   if ((int)posx[0] == foodx && (int)posy[0] == foody) {
    skor++;
    Console.Beep();
    //     Always 5, 6, 7, 8 or 9.
    // Console.WriteLine(r.Next(5, 10));  
    foodx = r.Next(1, Console.BufferWidth-2);
    foody = r.Next(2, Console.BufferHeight-2);
    posx.Add((int)posx[posx.Count-1]-dx);
    posy.Add((int)posy[posy.Count-1]-dy);
    
    if (skor % 10 == 0) {
     delaySpeed -= 10;
     if (delaySpeed <= 0) delaySpeed = 0;
     skor++;
    }
    Console.Beep();
   }
   
   

   if ((int)posx[0] == enemyx && (int)posy[0] == enemyy) {
    selesai = true;
   }
   enemyTimer--;
   if (enemyTimer <= 0) {
    enemyx = r.Next(1, 80);
    enemyy = r.Next(2, 23);
    enemyTimer = 150;
   }
   
   Console.SetCursorPosition(1, Console.BufferHeight-1);
   if (Console.KeyAvailable) key = Console.ReadKey();
   if (key.Key == ConsoleKey.Escape) selesai = true;
   
   if (key.Key == ConsoleKey.UpArrow) {
    if (dy != 1) {
     dy = -1;
     dx = 0;
    }
   }
   else if (key.Key == ConsoleKey.DownArrow) {
    if (dy != -1) {
     dy = 1;
     dx = 0;
    }
   }    
   else if (key.Key == ConsoleKey.LeftArrow) {
    if (dx != 1) {
     dy = 0;
     dx = -1;
    }
   }    
   else if (key.Key == ConsoleKey.RightArrow) {
    if (dx != -1) {
     dy = 0;
     dx = 1;
    }
   }    
   else if (key.KeyChar == ' ') {
    // spasi untuk pause
    Console.SetCursorPosition(35, 0);
    Console.Write("PAUSED");    
    key = new ConsoleKeyInfo();
    do {
     key = Console.ReadKey();
     Console.Write("\b \b");
    } while (key.KeyChar != ' ');
    key = new ConsoleKeyInfo();
   }
   
   Thread.Sleep(delaySpeed);
  }
  
  for (i=10; i<20; i++) {
   for (j=15; j<55; j++) {
    Console.SetCursorPosition(j, i);
    if (i==10 || i == 19 || j == 15 || j == 54)
     Console.Write("*");
    else 
     Console.Write(" ");
   }
  }
  Console.SetCursorPosition(30, 12);
  Console.Write("GAME OVER");

  Console.SetCursorPosition(17, 14);
  Console.Write("YOUR SCORE : " + skor);
  
  Console.SetCursorPosition(16, 20);
  Console.Write("f = food");
  Console.SetCursorPosition(16, 21);
  Console.Write("e = enemy");
  
  Console.SetCursorPosition(1, Console.BufferHeight-1);
  Console.ReadKey();
 }
}  // end of code

if you use windows 7, you can compile by typing :
c:\Windows\Microsoft.NET\Framework\v2.0.50727\csc file_name.cs

Wednesday, June 21, 2017

Change desktop wallpaper to all client computer

1. open group policy management


2. Right click and edit on the Default Domain Policy


3. Select User configuration - Policies - Administrative templates - Desktop - Desktop


4. Double click Desktop wallpaper on the right pane
 

5. Place your background image file on a shared folder.


6. [I don't know this command is necessary or not] Run : gpupdate /force

7. Logoff and login again (Or restart) on the client computer



My environment :
Server : windows server 2008 (domain controller)
Client : windows XP (join domain)