Você está na página 1de 4

sqliteDataBase

=========================
SQlitopenHelper
SqliteDatabase
myclass extends SqliteOpenHelper

create table if not exists student(id integer primary key autoincrement,name


text,age integer);
mainActivity
==========================
package com.example.lab.sqlitetest;

import android.content.ContentValues;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


EditText et_name,et_age;
DbHelper helper;
TextView result_data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name=findViewById(R.id.name);
et_age=findViewById(R.id.age);
result_data=findViewById(R.id.result);
helper=new DbHelper(this);
}

public void save(View view)


{
String name=et_name.getText().toString();
int age= Integer.parseInt(et_age.getText().toString());
ContentValues cv=new ContentValues();
cv.put(DbHelper.COL_1,name);
cv.put(DbHelper.COL_2,age);
long i=helper.insertData(cv);
if (i>0){
Toast.makeText(this,""+i+" inserted
successfully",Toast.LENGTH_SHORT).show();
}
}

public void load(View view)


{
Cursor c=helper.retrive();
StringBuffer sb=new StringBuffer();
while (c.moveToNext()){
sb.append(c.getInt(0)+" "+c.getString(1)+" "+c.getInt(2)+"\n");
}
result_data.setText(sb.toString());
}
}
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"
tools:context="com.example.lab.sqlitetest.MainActivity">

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

<EditText
android:id="@+id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint=" enter your age"

/>

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="save"
android:id="@+id/save"
android:text="save"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="load"
android:id="@+id/load"
android:text="load"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/result"
android:layout_width="364dp"
android:layout_height="700dp"
android:text="result"
android:textColor="@color/colorAccent"
android:textStyle="bold" />

</ScrollView>
</LinearLayout>
Db helper.java
======================================================
package com.example.lab.sqlitetest;

import android.annotation.TargetApi;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Build;

/**
* Created by LAB on 2/16/2018.
*/

public class DbHelper extends SQLiteOpenHelper


{
public static final String DATABASE_NAME="svec";
public static final String TABLE_NAME="student";
public static final String COL_0="id";
public static final String COL_1="name";
public static final String COL_2="age";
public static final String QUERY="create table if not exists
"+TABLE_NAME+"("+COL_0+" integer primary key autoincrement,"+COL_1+" text,"+COL_2+"
integer);";
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(QUERY);

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1)
{
db.execSQL("drop table "+TABLE_NAME);
onCreate(db);

}
public long insertData(ContentValues cv)
{
SQLiteDatabase db=getWritableDatabase();
long i=db.insert(TABLE_NAME,null,cv);
return i;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public Cursor retrive(){
SQLiteDatabase db=getReadableDatabase();
Cursor c= db.rawQuery("select *from "+TABLE_NAME,null,null);
return c;
}
}

Você também pode gostar