Skip to content

Commit 0460d74

Browse files
committed
Database in Andriod App
Learn inbuilt support of SQLiteDatabase Available in android.sqlite package
1 parent 4605958 commit 0460d74

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

Diff for: databasedemo/MainActivity.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.lenovo.databasedemo;
2+
3+
import android.database.Cursor;
4+
import android.database.sqlite.SQLiteDatabase;
5+
import android.support.v7.app.AppCompatActivity;
6+
import android.os.Bundle;
7+
import android.util.Log;
8+
9+
public class MainActivity extends AppCompatActivity {
10+
11+
@Override
12+
protected void onCreate(Bundle savedInstanceState) {
13+
super.onCreate(savedInstanceState);
14+
setContentView(R.layout.activity_main);
15+
16+
try {
17+
SQLiteDatabase mydatabase= this.openOrCreateDatabase("Users",MODE_PRIVATE,null);
18+
19+
mydatabase.execSQL("CREATE TABLE IF NOT EXISTS users(name VARCHAR,age INT(3))");
20+
mydatabase.execSQL("INSERT INTO users(name, age) VALUES ('Akshay',87)");
21+
mydatabase.execSQL("INSERT INTO users(name, age) VALUES ('saksham',2)");
22+
23+
Cursor c = mydatabase.rawQuery("SELECT * FROM users",null);
24+
25+
int nameIndex= c.getColumnIndex("name");
26+
int ageIndex = c.getColumnIndex("age");
27+
28+
c.moveToFirst();
29+
while(c!=null){
30+
Log.i("name",c.getString(nameIndex));
31+
Log.i("age",Integer.toString(c.getInt(ageIndex)));
32+
c.moveToNext();
33+
}
34+
35+
36+
}catch (Exception e)
37+
{
38+
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)