//The table name is redefined by name, and the table name is the class name of the entity class by default//redefining the ID of a table by id= "_id"@Table (name = "User_table", id = "_id") Public classUserentityextendsModel {//@Column Note indicates that the field is a field in a database table//to redefine the name of a field in a table in a database by name= "name"@Column (name = "Name") PrivateString username; @ColumnPrivate intAge ; @Column//new Field PrivateString Nickname; Publicuserentity () {} PublicUserentity (intAge , String username) { This. Age =Age ; This. Username =username; } PublicString Getnickname () {returnnickname; } Public voidSetnickname (String nickname) { This. Nickname =nickname; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } PublicString GetUserName () {returnusername; } Public voidSetusername (String username) { This. Username =username; } @Override PublicString toString () {return"userentity{" + "age=" + Age + ", username= '" + username + ' \ ' + '} '; }}
Public class extends Application { @Override publicvoid onCreate () { super . OnCreate (); // initializing the database framework Activeandroid.initialize (this);} }
/*** 1. Add dependencies * 2. Initialize Activeandroid * 3 in the Application class. Create an entity class that inherits from Model * 4. Create an instance of an entity class, and then call the instance's Save method to save the data * <p/> * <p/ > * ********************* Database Upgrade Step **************************** * 1. Modify the Entity class, add new fields, add @column annotations * 2. Modify the database version number, The new version number is the old version number plus 1 * 3. Write the database migration script, create the Migrations folder in the Assets folder, and then create the migration script file in the Migrations folder * The file name of the migration script file is ' the most recent database version number. ' SQL ', For example, the latest version number is 3, the name of the migration script is 3.sql*/ Public classMainactivityextendsappcompatactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); } Public voidaddData (view view) {userentity userentity=NewUserentity (56, "John Doe"); Userentity.setnickname ("Lee 4,123"); //call the Save method to save Userentity to the databaseUserentity.save (); } Public voiddeleteData (view view) {/*********** Delete data based on ID ***************/ //Delete Data//Delete data with ID 5//Userentity.delete (Userentity.class, 5);Delete delete =NewDelete (); //Delete a user named John DoeDelete.from (userentity.class). WHERE ("Name= ' John Doe '"). Execute (); } Public voidupdatedata (view view) {//Modifying DataUpdate update =NewUpdate (userentity.class); //Change the Name field of all users with name ' John Doe ' to ' Zhang San 'Update.set ("Name= ' Zhang San '"). where ("Name= ' John Doe '"). Execute (); } Public voidselectdata (view view) {//Querying DataSelect select =NewSelect (); //querying for a user with name John Doelist<userentity> list = Select.from (userentity.class). WHERE ("Name= ' John Doe '"). Execute (); for(userentity userentity:list) {LOG.D ("GOOGLE_LENVE_FB", "Selectdata:" +userentity.tostring ()); } }}
"Framework" Activeandroid database operations