How to Create a Database in Android

How to create a SQL database for android?

You just have to add a sqlite file with the database to the app resources if i remember right, and use it with a DBHelper class like these but adapted to your app needs.

I personally create and edit the sqlite files with this Firefox extension.

Fails to create database in Android sqlite

Seems like you're missing databases folder. Try:

public static void CreateDb(String dbPath){
File f = new File(dbPath);
f.getParentFile().mkdirs();
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbPath, null);
// ...
}

how to create simple SQLite database in android with cursor adaptor?

I developed a sample project which i used in my main application. I hope that helps you too.

DataBaseHandler.java

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;

public class DataBaseHandler extends SQLiteOpenHelper {

public String BooksTable="Books";
public String Book_Id="id";
public String BookAuther="BookAuther";
public String BookTitle="BookTitle";

private final int DB_Version=1;
public DataBaseHandler(Context context,String DBName,int DBVersion){
super(context, DBName,null,DBVersion);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("Create Table " + BooksTable + "(" + Book_Id + " INTEGER PRIMARY KEY, " + BookAuther + " Text, " + BookTitle + " Text);");
}

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

}
public void addBook(Book book){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(Book_Id,book.getId());
cv.put(BookAuther,book.getAuthor());
cv.put(BookTitle,book.getTitle());

db.insert(BooksTable, null, cv);
db.close();
}

public ArrayList<Book> fetchAllBooks(){
ArrayList<Book> booksList = new ArrayList<Book>();
SQLiteDatabase db=this.getReadableDatabase();
Cursor c= db.query(BooksTable, null, null, null, null, null, null);
c.moveToFirst();
while (!c.isAfterLast()){
Book book = new Book();
book.setId(c.getInt(0));
book.setAuthor(c.getString(1));
book.setTitle(c.getString(2));
booksList.add(book);
c.moveToNext();
}
db.close();
return booksList;
}
}

Book.java

public class Book {
private int id;
private String author;
private String title;

public void setId(int id) {
this.id = id;
}

public void setAuthor(String author) {
this.author = author;
}

public void setTitle(String title) {
this.title = title;
}

public int getId() {
return id;
}

public String getAuthor() {
return author;
}

public String getTitle() {
return title;
}
}

MainActivity.java

DataBaseHandler db=new DataBaseHandler(getApplicationContext(),"DB_Name",1);
Book book=new Book();
book.setId(1);
book.setTitle("My Title");
book.setAuthor("Name of Author");
db.addBook(book);

How to create a Local Database in Android?

You should read something about the SQLiteOpenHelper. It is the much better approach to use a SQLite Database in Android.

But for your case, you forgot to wrap your text values in '

db.execSQL("insert into student( name , roll_no , semester )" + " values "+
"('"+ st_Name + "'," + st_RollNo + ",'" +st_sem + "');");

The android approach to avoid such problems would be to set your values to insert in a ContentValues Object and use database's insert function:

ContentValues values = new ContentValues();
values.put("name", st_Name);
values.put("roll_no", st_RollNo);
values.put("semester", st_sem);
db.insert("student", null, values);

Or you could use Prepared Statements:

SQLiteStatement stmt = db.compileStatement("insert into student( name , roll_no , semester ) values (?,?,?);");
stmt.bindString(1, st_Name);
stmt.bindInt(1, st_RollNo);
stmt.bindString(1, st_sem);
stmt.execute();

The Insert function as well as using prepared Statements are the more secure way to insert data. They will take of escaping Strings and putting them into the right format.

Create SQLite database in android

Better example is here

 try {
myDB = this.openOrCreateDatabase("DatabaseName", MODE_PRIVATE, null);

/* Create a Table in the Database. */
myDB.execSQL("CREATE TABLE IF NOT EXISTS "
+ TableName
+ " (Field1 VARCHAR, Field2 INT(3));");

/* Insert data to a Table*/
myDB.execSQL("INSERT INTO "
+ TableName
+ " (Field1, Field2)"
+ " VALUES ('Saranga', 22);");

/*retrieve data from database */
Cursor c = myDB.rawQuery("SELECT * FROM " + TableName , null);

int Column1 = c.getColumnIndex("Field1");
int Column2 = c.getColumnIndex("Field2");

// Check if our result was valid.
c.moveToFirst();
if (c != null) {
// Loop through all Results
do {
String Name = c.getString(Column1);
int Age = c.getInt(Column2);
Data =Data +Name+"/"+Age+"\n";
}while(c.moveToNext());
}

how to create a database and fill it in sqlite android?

The spacing in the create table query is wrong.
Try this.

String query = "CREATE TABLE " + TABLE_USERS + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COLUMN_USERNAME + " Text,"
+ COLUMN_PASSWORD + " Text);";

Also write a separate method to insert a record. Don't do it in onCreate.

Cheers



Related Topics



Leave a reply



Submit