Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    APIs
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> Android >> Create Table in Android Database
Create Table in Android Database
Create Table in Android Database


by Rohit Kesharwani on 10/27/2011 8:55:22 PM

Views: 3964       Comments: 0

Create Table in Android Database

In this article I am going to explain how create a database in an Android application and how create table in the database.

·         Start a new project named TableDemo.

·         Open res/layout/main.xml and insert the following:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">

<TextView

       android:id="@+id/txtView"

       android:layout_marginLeft="100dp" 

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:text="@string/hello"/>

</LinearLayout>

·         Open the Activity file and insert the following code:

import java.util.Locale;

import android.app.Activity;

import android.database.sqlite.SQLiteDatabase;

import android.os.Bundle;

import android.widget.TextView;

 

public class DatabaseActivity extends Activity {

   

       private String table_name="StudentInfo";

      

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        SQLiteDatabase db;

       

        // create or open database file

        db = openOrCreateDatabase("Test.db" , SQLiteDatabase.CREATE_IF_NECESSARY, 
                                                                             
null);

        db.setVersion(1);

        db.setLocale(Locale.getDefault());

        db.setLockingEnabled(true);

       

        // creating table in database

        db.execSQL("CREATE TABLE IF NOT EXISTS "+table_name+" " +

                        "( sid INTEGER PRIMARY KEY AUTOINCREMENT," +

                        "  name TEXT" +

                        "  age INTEGER" +

                        "  course TEXT ); ");

       

        // capture element id from layout

        TextView txtView=(TextView)findViewById(R.id.txtView);

        txtView.setText("Table created");

    }

}

·         Run the application.
The output should look like below:

Create Table in Android Database

TextView display a message Table created when you run an application.

·         Check the created table in the adb shell:

In order to check the tables and database information from the database, you can use adb shell provided by Android SDK:

1)      Run the cmd (command prompt).

2)      Go to the following location:
 d:\android-sdk_r13-windows\android-sdk-windows\platform-tools> (you have to user your own location).

3)      Type adb shell and press enter, a # symbol will display.

4)      Type sqlite3 /data/data/ [package name]/databases/database-name (Test.db) and press Enter.
For e.g. sqlite3 /data/data/com.android.databaseDemo/databases/Test.db

5)      Then type .tables in order to see the table present in the database.

6)      It will show you a list tables present in the database.


Thanks for reading this article. I think this will help you a lot.

Report Abuse Form
Reason:    
 

Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by Rohit KesharwaniRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 2906
Advertisement
MindStick DataConver
Advertise with Us
  
Copyright © 2013MindStick. All Rights Reserved.