Here you'll get help for all programming languages like android,php, html, css, bootstrap, javaScript, java etc...

Insert Data into SQLite Database (Android)

In this example i'm going to show you how to insert data into sqlite database.




activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    tools:context="com.example.malik.sqlitepractise.MainActivity">

    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter name" />

    <EditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Email" />

    <EditText
        android:id="@+id/contact"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Contact" />

    <Button
        android:id="@+id/insert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:text="Insert" />
</LinearLayout>



MainActivity.java


package com.example.malik.sqlitepractise;

import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private EditText name, email, contact;
    private Button insert;

    DatabaseHelper databaseHelper;
    SQLiteDatabase sqLiteDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        name = (EditText) findViewById(R.id.name);
        email = (EditText) findViewById(R.id.email);
        contact = (EditText) findViewById(R.id.contact);

        insert = (Button) findViewById(R.id.insert);

        insert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String NAME = name.getText().toString();
                String EMAIL = email.getText().toString();
                String CONTACT = contact.getText().toString();

                databaseHelper = new DatabaseHelper(MainActivity.this);
                sqLiteDatabase = databaseHelper.getWritableDatabase();
                databaseHelper.insertDataMethod(NAME,EMAIL,CONTACT,sqLiteDatabase);
                Toast.makeText(getApplicationContext(),"Data Inserted",Toast.LENGTH_LONG).show();
                databaseHelper.close();


                name.setText("");
                email.setText("");
                contact.setText("");


            }
        });

    }
}




DatabaseHelper.java


package com.example.malik.sqlitepractise;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * Created by malik on 14/12/2017.
 */

public class DatabaseHelper extends SQLiteOpenHelper {


    public static final String NAME = "name";
    public static final String EMAIl = "email";
    public static final String CONTACT = "contact";

    public static final String TABLE_NAME = "personTable";

    public static final String DATABASE_NAME = "personDatabase";
    public static final int DATABASE_VERSION = 1;

    public static final String QUERY_FOR_CREATE_TABLE =
            "create table "+TABLE_NAME+"("+NAME+" text,"+EMAIl+" text,"+CONTACT+" text);";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        Log.d("SQLite", "Database created");

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(QUERY_FOR_CREATE_TABLE);
        Log.d("SQLite", "Table created");
    }

    public void insertDataMethod(String name, String email, String contact, SQLiteDatabase sqLiteDatabase){
        ContentValues contentValues = new ContentValues();
        contentValues.put(NAME,name);
        contentValues.put(EMAIl,email);
        contentValues.put(CONTACT,contact);
        sqLiteDatabase.insert(TABLE_NAME,null,contentValues);
        Log.d("SQLite", "Data inserted");


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

User Help

If you want any source code or any other help just contact me at malik_himani@ymail.com. Your requirement's code will be uploaded in blog soon.