Skip to main content

Room Data base config insert update delete

for use room data base in android you need to setup faw things


  • first you have to add two deandancy in your gradel file 

implementation "android.arch.persistence.room:runtime:1.0.0-beta2"annotationProcessor "android.arch.persistence.room:compiler:1.0.0-beta2"

then make a entity class and dao class in your file structure 




Data base class 


package com.plzf1.rollearn.database;

import android.arch.persistence.room.Database;
import android.arch.persistence.room.RoomDatabase;

@Database(entities = User.class , version = 1)
public abstract class MyDataBase extends RoomDatabase {

    public abstract UserDao userDao();
}



Entity Class



android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.PrimaryKey;

@Entity(tableName = "user")
public class User
{
    @PrimaryKey(autoGenerate = true)
    private int userid;
    @ColumnInfo(name = "user_name")
    private  String username;
    @ColumnInfo(name = "user_mobile")
    private String mobile;
    @ColumnInfo(name = "user_balance")
    private int userblance;

    public int getUserid() {
        return userid;
    }

    public void setUserid(int userid) {
        this.userid = userid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public int getUserblance() {
        return userblance;
    }

    public void setUserblance(int userblance) {
        this.userblance = userblance;
    }
}


User Dao


package com.plzf1.rollearn.database;
import android.arch.persistence.room.Dao;import android.arch.persistence.room.Insert;import android.arch.persistence.room.Query;import android.arch.persistence.room.Update;
@Daopublic interface UserDao {


    @Insert    void insert(User user);
    @Update    void updateAmout(User user);
    @Query("SELECT * FROM user WHERE userid=:id")
    void loadSingle(String id);}


Comments

Popular posts from this blog

how to create chat gpt app in android with java code

  I'd be happy to provide you with some sample Java code for a chat GPT app in Android, however it is important to note that creating a complete and functional app requires a solid understanding of Android development, Java and OpenAI API, and also the code I provide here is just a skeleton or a sample of what it would look like in practice. You would need to import the OpenAI API library and configure it with your API key. Here is an example of how you might use the OpenAI Java client library to send user input to the GPT-3 model and receive the generated response: import openai.OpenAI; public class ChatGPT { private OpenAI openai; private String apiKey = "YOUR_API_KEY"; public ChatGPT() { openai = new OpenAI(apiKey); } public String generateResponse(String input) { String response = ""; try { response = openai.completions().create() .engine("text-davinci-002") ...

How to implement the Android ActionBar back button

Add this code in your on create activity Method  @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled( true ); getSupportActionBar().setDisplayShowHomeEnabled( true ); } then call this method @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id. home : finish(); return true ; default : return super .onOptionsItemSelected(item); } } this is default support action bar if you want your own then use own tool bar in your xml thanks
                             Top Troubleshooting in Android   If  you are android developer and you don't how to troubleshot your code so i am sharing with you some tips which will help you   ctrl + b for back trace your code it will redirect you to where you use of your code  ctrl+alt+ ← come back to privies use of code and if its not working then use alt + ← ctrl+ click it will redirect you to use code