Showing posts with label Database. Show all posts
Showing posts with label Database. Show all posts

June 10, 2020

Microsoft Azure SQL Server Read Only User Creation Deletion

Hi folks,

Today I will explain, how you can easily manage the readonly (view) users in Azure Microsoft SQL Server

Procedure

In Azure SQL Server

CREATE LOGIN [testuser] WITH PASSWORD = 'random_p@$$w0rd';

For master and each DB

Note: You must create the user in master db before creating in other databases
CREATE USER [testuser] FOR LOGIN [testuser]   
    WITH DEFAULT_SCHEMA = [dbo];  
GO

Grant Connect permission

GRANT CONNECT TO [testuser]
GO

Give datareader role to read (view) only

ALTER ROLE db_datareader ADD MEMBER [testuser]
GO

Drop user in DB

DROP USER [testuser]
GO

Drop user in Azure SQL

DROP LOGIN [testuser];
GO

Please send your comments and feedback to psrdotcom@gmail.com

April 14, 2020

Microsoft SQL Server 2019 Express Docker image Example

Hi Folks,

Today I am going to explain the procedure for connecting to a Microsoft SQL Server 2019 Express edition docker image

Pre-requisites

  1. Docker Desktop
  2. Windows OS
  3. Powershell/command prompt

Procedure

Get and run docker image

> docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=<Your_Password>" -e "MSSQL_PID=Express" --name "<Your_SQL_Server_Name>" -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latest

Command information

Remove the "MSSQL_PID=Express" to run other version of SQL Server
Replace 2019 with required SQL Server version
Password should be atleast 8 characters with capital, small, numeric, special character combination
Use different port if you already have a local sql server
Name should not contain spaces

Check for docker container

> docker ps

You should able to see the container with your SQL Server name at the end in running status

Connect to SQL Server

docker exec -it "<Your_SQL_Server_Name>" /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "<Your_Password>"

You should be able to see "1>" prompt

Command information

Use the SQL Server name or user the container ID

Use database and play around with table(s)

Important
Multiple commands can be entered one after one, but to execute the set of command(s), you need give "GO" command.

Create Database

CREATE DATABASE SampleDB
GO

List all databases

SELECT Name from sys.Databases
GO

Start using the database

USE SampleDB
GO

Create table

CREATE TABLE UserInfo ( Id INT, Name VARCHAR(64))
GO

Insert values

INSERT INTO UserInfo (1, 'ABC')
GO

Retrieve table contents

SELECT * FROM UserInfo
GO

Exit from SQL Server

QUIT

Hope you are able to run the SQL Server Docker image.

Please send your feedback and comments to psrdotcom@gmail.com

July 29, 2019

Docker postgres db to local volume/drive for persistent storage permanently

Hi all,

Today, I would be explaining the way to maintain the DB with data persistent when connecting to docker.

System Status

Postgresql DB container is in Docker
Windows 10 PC
Linux containers

Issue

Whenever I restart docker/PC the database wiped off due to container is stopped.

Solution

Create a docker volume

Syntax: docker volume create <>
Example: docker volume create pgdata

Start the container with volume option

Syntax: docker run --rm --name <> -e POSTGRES_PASSWORD=<> -d -p 5432:5432 -v <>:/var/lib/postgresql/data <>
Example: docker run --rm --name pgcontainer -e POSTGRES_PASSWORD=MyPassword -d -p 5432:5432 -v pgdata:/var/lib/postgresql/data postgres

Now, you can run all your database commands

Upon restart also, your data should be safe.

Thanks for reading the blog post.

Please send your feedback and comments to psrdotcom@gmail.com

November 21, 2018

Connect to remote Oracle database server from RedHat Linux 7 using Oracle InstaClient

Hi friends,

I have come across a situation, where I need to connect to remote Oracle database server from RedHat Enterprise Linux (RHEL) 7. I found a way of using the Oracle InstaClient instead of installing the full Oracle DB which internally consists of Client.

Pre-requesites


  1. Linux mahcine (I tried with RHEL 7) with following softwares
    • Java (JAVA_HOME should be available)
    • Development Tools (RHEL 7 tools like gcc etc.)
  2. Another machine where Oracle server is installed with the following config
    • Database schema should be available
    • Users should be available instead of system/sysdba users
  3. Both machines shall be reachable to each other via ping.

Note: You should know the database server SID/service name.

Procedure

Download


  1. Download the insta client from Oracle Official Page
  2. In my case, I have downloaded the "Instant Client for Linux x86-64"
  3. Accept the license agreement
  4. Click "oracle-instantclient18.3-basic-18.3.0.0.0-1.x86_64.rpm" to download
  5. Optionally, you can download the "oracle-instantclient18.3-sqlplus-18.3.0.0.0-1.x86_64.rpm" to have sqlplus command line utility for testing
  6. Alternatively, you can download the zip files and download the same to install.
  7. In my case, I am going with RPMs.

Install


  • Install the downloaded RPMs
    • #> yum install oracle-instantclient18.3-basic-18.3.0.0.0-1.x86_64.rpm
    • #> yum install oracle-instantclient18.3-sqlplus-18.3.0.0.0-1.x86_64.rpm
  • While installing, it asks for locations, accept the defaults by pressing the "Enter" button

Note: If any dependencies are there, please install those.

Environment Variables


  • Create a oracle_env.sh file and setup your variables
    • #> gedit /etc/profile.d/oracle_env.sh
    export ORACLE_HOME=/usr/lib/oracle//client/
    export TNS_ADMIN=/usr/lib/oracle//client/
    export PATH=$ORACLE_HOME/bin:$PATH
    export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH



    • Save your file
    • Source your file with following command
      • #> source /etc/profile.d/oracle_env.sh

    tnsnames.ora


    • Create tnsnames.ora file in your $ORACLE_HOME with following syntax

    localsid = (DESCRIPTION =
        (ADDRESS = (PROTOCOL = TCP)(HOST = )(PORT = ))
        (CONNECT_DATA =
          (SID = )
        )
      )

    • Instead of SID, you can use SERVICE_NAME also.

    Connect


    • You can test the connection
      • #> sqlplus @


    You should be able to connect to your remote database.

    Hope, it is useful for you.

    Please send your comments and feedback to psrdotcom@gmail.com

    March 02, 2017

    Oracel Database: Delete all user objects (Tables, Views, Procedures, Functions, Sequences, Types, Packages)

    Hi friends,

    I have come across a situation, where I need to delete all the user objects i.e. Tables, Views, Procedures, Functions, Sequences, Types, Packages.

    Please find the link below for the SQL script, which does the job for you.

    It's my public GitHubGist URL, where anyone can share the code snippets.

    https://gist.github.com/psrdotcom/b61f8b401d4992428ecd8fb8225436c4

    Advantage:
    You can delete the user after deleting all user objects by logging into SYSTEM user.

    Please send your review and feedback to psrdotcom@gmail.com


    September 16, 2013

    Convert MS-Access to SQLite Database

    If you are familiar with MS-Access database creation and wants to covert your database to SQLite without putting all your efforts again.

    If you database already exists in MS-Access and you want to have that in SQLite for mobile development. Its now a cake-walk like conversion

    Here is an easy process to do that.

     

    • Download from https://code.google.com/p/mdb-sqlite/downloads/list
    • Extract the file, you will be getting mdb-sqlite folder with below file structure
    • image
    • Open Command Prompt and navigate to mdb-sqlite folder
    • Run “ant dist” command (without quotes")
    • image
    • Once the build successful, it will generate dist\main.jar and dist\mdb-sqlite.jar files
    • You can use these jar files to covert your ms-access database to sqlite database with the following command
    • Syntax :-
      • java -jar dist\mdb-sqlite.jar source.mdb output.sqlite
    • Example :-
      • java –jar dist\mdb-sqlite.jar employee.mdb employee.sqlite

     

    References

    https://code.google.com/p/mdb-sqlite/

    Blogger Labels: Convert,SQLite,Database,creation,efforts,Here,Download,code,Extract,folder,Open,Command,Prompt,quotes,Once,files,Syntax,output,Example,employee,References,google,dist,java

    January 27, 2012

    SQLite Database Instalation and Usage tutorials in Mobile Application Developement

    Hi friends,

    I would like share something related to Mobile Application Development.

    SQLite Command Line Tool

    • Download the sqlite command line tool
    • Opening database with sqlite
    • sqlite_dir_path> sqlite databasefile.db 
      • Note: Save sqlite database file with an extension .db

    To find the commands help, please go through the following links
    http://www.sqlite.org/sqlite.html
    http://zetcode.com/databases/sqlitetutorial/tool/
    http://souptonuts.sourceforge.net/readme_sqlite_tutorial.html

    SQLite Database Browser

    • Download the lastest sqlite browser files from SourgeForge
    • Extract the files and execute the SQLite Database Browser.exe
    • SQLite Database Browser will be opened

    To find more information on usage, please go through the following links
    http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
    http://www.vogella.de/articles/AndroidSQLite/article.html

    For queries, please contact psrdotcom@gmail.com

    January 17, 2012

    Take Nokia Phone Contacts (Phonebook) backup in Excel Spreadsheet or .csv file by using OVI PC Connectivity

    Hi friends,

    Today I have come up with phonebook backup in Nokia. I previously used very old nokia model which doesn't contain the Nokia PC/Ovi Suite connectivity. So, I used to take backup of my contacts in excel sheet by doing data entry. But now most of phones does have the facility to connect to Nokia Ovi/PC Suite to take the backup and maintain their data in Computer(PC).

    Here, I have taken backup of my contacts in PC and converted that to Excel sheet to quick access of my contacts.

    Pre-Requisites:
    1. Make sure that, your phone does support Mobile to PC connectivity via Nokia Ovi/PC Suite
    2. Download and install Nokia Ovi/PC Suite
    3. Make sure that, you have the PC connectivity cable to the corresponding mobile
    4. Make sure that, all your contacts are on device(mobile) memory instead of SIM
      • Note: Copy all the SIM contacts to device(mobile)
    Procedure:
    • Connect your mobile to PC
    • Select "Nokia Ovi PC Suite" connection from your mobile
    • After successful connection, you will get the similar screen like below
    • Select "Contacts" from the image icons on top of the window
    • Now, all your contacts will be displayed on the 2nd pane(column) in the Window as shown below
    • Select all the contacts (Note: First select any one of contact and do the any one of the below)
      • Option 1: Use Keyboard shortcut Control + A
      • Option 2: Right click on the contact and choose "Select All" option
      • Option 3: Select "Edit" menu and choose "Select All" option
    • Now, you can "Drag and Drop" the selected contacts to PC contacts  
      • Hint: Drag and drop the content to "Contacts" (Red Circle)
    By now, you have successfully taken the backup of your contacts to PC

    View/Save the contacts in Spreadsheet/.csv file
    • Download the file GrabMyContacts from this link 
    • Install it in any folder
    • Run the "grabmycontacts.exe", you will get the following screen

    • Default "Step 1 - Search for your contacts file" tab will be selected
    • Click the "Search for it" button, it will automatically searches your Nokia Ovi contacts database file as shown below
      • Click on "Yes"
    • Now, choose the "Step 2 - Choose and display your data" tab
      • Click on "Push this button to display your contacts"
      • Now, it will display all your contacts in the sheet
    • Finally, Choose "Step 3 - Export to Excel" tab
      • Click on "Finished ! Now Export to Excel !" button
      • It will show, your file named "myovicontacts.xml" has been saved on desktop
      • Click on "Yes"
      • Then, it will ask you to open the file Excel, as shown in the below screen
      • Click on "Yes"
    • If the default program for the xml is not Excel then it will open with the default program. 
    • Open the XML file in Excel:
    • To see your contacts in excel, open the new Excel workbook and "Drag and Drop" the "myovicontacts.xml" in it
    • Remove the unnecessary columns and save your contacts as "Excel Workbook"/".csv" file format

    December 19, 2010

    Insert and Display image BLOB in JSP page

    Hi everyone,

    Today we just tried to insert jpeg file and displaying that file in one html page

    Source Code:
    file name: img_uploadanddisplay.jsp
    <%@ page import="java.sql.*"%>
    <%@ page import="java.io.*, java.util.*, java.lang.*"%>

    <% ResultSet rs=null;
    Connection con=null;
    Statement stmt=null;
    PreparedStatement psmt,psmt1;
    Blob blobfile = null;
    byte [] imgdata = null;
    int status = 0;
    boolean del_status = false;
    OutputStream os = null;
    try
    {
    String req_id=request.getParameter("id");
    System.out.println("Entered"+req_id); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("Jdbc:Odbc:BTSJAVA","System","vijay"); stmt=con.createStatement();
    psmt = con.prepareStatement("insert into img_temp values(?,?)");
    int id_val = 1;
    File img=new File("D:/photos/passport/PP.jpg");
    FileInputStream fis= new FileInputStream(img);
    psmt.setInt(2,id_val);
    psmt.setBinaryStream(1,(InputStream)fis,(int)(img.length()));
    status = psmt.executeUpdate();
    //System.out.println("Trying to insert data");
    if(status>0)
    System.out.println("Success");
    else
    System.out.println("Unsuccess");

    stmt.close();

    stmt = con.createStatement();
    rs = stmt.executeQuery("select imgfn from img_temp where id = 1"); //+Integer.parseInt(req_id));

    response.setContentType("image/jpeg");
    os = response.getOutputStream();

    while(rs.next())
    {
    System.out.println("Getting Data");

    os.write(rs.getBytes("imgfn"));

    System.out.println("Got Data");
    }

    os.flush();
    os.close();
    }
    catch(Exception e)
    {
    out.println(e);
    e.printStackTrace();
    }
    %>

    Create a html file with the following content

    image tag src="img_uploadanddisplay.jsp?id=1"


    Please send your feedback and queries to me.

    Featured Post

    Java Introdcution

    Please send your review and feedback to psrdotcom@gmail.com