Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • To create database and insert data in SQLite, in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 423
    Comment on it

    If you are looking for the code in android to create database and insert data in SQLite then follow the steps mentioned below:-

    1) Create a layout in which the data will be filled that will be inserted into database.

    activity_signup_form.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:orientation="vertical"
        tools:context="com.example.manish.loginform.SignupForm">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tvSignUp"
            android:textSize="@dimen/fontSize"
            android:textStyle="bold|italic"
            android:layout_gravity="center_horizontal"/>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="2">
            <EditText
                android:id="@+id/etFirstName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/etFirstName"
                android:layout_weight="1"
                />
            <EditText
                android:id="@+id/etLastName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/etLastName"
                android:layout_weight="1"
                />
        </LinearLayout>
        <EditText
            android:layout_marginTop="@dimen/fontSize"
            android:id="@+id/etMailid"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/etSUser"
            />
        <EditText
            android:layout_marginTop="@dimen/fontSize"
            android:id="@+id/etSPassword"
            android:layout_width="match_parent"
            android:password="true"
            android:layout_height="wrap_content"
            android:hint="@string/etSPassword"
            />
        <EditText
            android:layout_marginTop="@dimen/fontSize"
            android:id="@+id/etSConferm"
            android:layout_width="match_parent"
            android:password="true"
            android:layout_height="wrap_content"
            android:hint="@string/etSConferm"
            />
    
        <Button
            android:id="@+id/btnSubmit"
            android:layout_marginTop="@dimen/fontSize"
            android:layout_gravity="center_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btnSubmit"/>
     </LinearLayout>
    

    2) Now initialize all widgets in java file

    SignupForm.java

    public class SignupForm extends AppCompatActivity
    {
        boolean name=false;
        boolean match=false;
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_signup_form);
    
            Button btnSubmit=(Button)findViewById(R.id.btnSubmit);
            final EditText etFirstName,etLastName,etMailid,etPaswd,etConferm;
            etFirstName=(EditText)findViewById(R.id.etFirstName);
            etLastName=(EditText)findViewById(R.id.etLastName);
            etMailid=(EditText)findViewById(R.id.etMailid);
            etPaswd=(EditText)findViewById(R.id.etSPassword);
            etConferm=(EditText)findViewById(R.id.etSConferm);
            btnSubmit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String first,last,user,pwd;
                    String password= etPaswd.getText().toString();
                    String confermp=etConferm.getText().toString();
                    user=etMailid.getText().toString();
                    String pattern="[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
                    if(user.isEmpty())
                    {
                        Toast.makeText(SignupForm.this,"enter mail-id",Toast.LENGTH_LONG).show();
                    }
                    else if(user.matches(pattern))
                    {
                        name=true;
                    }
                    else
                    {
                        name=false;
                        Toast.makeText(SignupForm.this,"incorrect mail-id",Toast.LENGTH_LONG).show();
                    }
                    if(password.equals(confermp)&&password.length()>6)
                    {
                        match=true;
    
                    }
                    else
                    {
                        Toast.makeText(SignupForm.this, "Password Dint match", Toast.LENGTH_LONG).show();
                        match=false;
    
                    }
                    if(match&&name)
                    {
    
                        first=etFirstName.getText().toString();
                        last=etLastName.getText().toString();
    
                        pwd=etConferm.getText().toString();
    
                        DatabaseHandler db=new DatabaseHandler(getApplicationContext());
                        try {
                            //This will check weather the username is existing or not
                                if(db.mail(user))
                                {
                                    Toast.makeText(SignupForm.this,"username already exists please try with another name",Toast.LENGTH_LONG).show();
    
                                 }
                                else
                                {
                                    db.insertLabel(first, last, user, pwd); //calling the function that insert data to sqlite database
                                    Toast.makeText(SignupForm.this, "inserted in Database", Toast.LENGTH_LONG).show();
    
                                }
                            }
                        catch (SQLException e)
                        {
                            e.printStackTrace();
                        }
                    }
    
                    }
                 });
        }
    }
    

    3) Now create a Databasehandler java file through which database query will be executed.

    DatabaseHandler.java

    public class DatabaseHandler extends SQLiteOpenHelper
    
    {
    
        private static final int DATABASE_VERSION = 1;
        private static final String DATABASE_NAME = "LoginDetails";
        private static final String TABLE_NAME = "Details";
        private static final String COLUMN_NAME1 = "firstname";
        private static final String COLUMN_NAME2 = "lastname";
        private static final String COLUMN_NAME3 = "mailid";
        private static final String COLUMN_NAME4 = "password";
    
    
    
        public DatabaseHandler(Context context)
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
    
        public void onCreate(SQLiteDatabase db)
        {
            System.out.println("in the Database");
            String CREATE_ITEM_TABLE = "Create table " +TABLE_NAME + "(" +COLUMN_NAME1+ " TEXT," +COLUMN_NAME2+ " TEXT," +COLUMN_NAME3+ " TEXT," +COLUMN_NAME4+ " TEXT)";
            db.execSQL(CREATE_ITEM_TABLE);
            System.out.println("Database created");
        }
    
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
            db.execSQL("Drop table if exists" + TABLE_NAME);
            onCreate(db);
        }
    
        public void insertLabel(String label1, String label2, String label3, String label4)
        {
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues values = new ContentValues();//Object created
            values.put(COLUMN_NAME1, label1);
            values.put(COLUMN_NAME2, label2);
            values.put(COLUMN_NAME3, label3);
            values.put(COLUMN_NAME4, label4);
            db.insert(TABLE_NAME, null, values);
            db.close();
        }
    public boolean login(String mailid, String password)throws SQLException
    {
        SQLiteDatabase db=this.getWritableDatabase();
        Cursor cursor= db.rawQuery("Select * from "+TABLE_NAME+" where mailid=? AND password=?",new String[]{username,password});
        if(cursor!=null)
        {
            if(cursor.getCount()>0)
            {
                return true;
            }
        }
    return false;
    }
        public boolean mail(String mail)throws SQLException
        {
            SQLiteDatabase db=this.getWritableDatabase();
            Cursor cursor=db.rawQuery("Select * from "+TABLE_NAME+" where mailid=?",new String[]{mail});
            if(cursor!=null)
            {
                if(cursor.getCount()>0)
                {
                    return true;
                }
            }
            return false;
        }
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: