首页 文章 精选 留言 我的

精选列表

搜索[visual studio code],共10000篇文章
优秀的个人博客,低调大师

Android Studio 第四十九期 - Sqlite数据库四种写法

最近在看撸撸代码,他自己创了一种,网上有三种,分别是OpenDroid,greenDao,sugar,Sqlite原生写法,感觉都差不多,个人使用最优的是greenDao,下面开始介绍: 1.OpenDroid:优点是简单易懂,缺点是没有联合查询,DeleteAll细节操作: assets: 1 2 3 4 5 6 7 8 9 10 11 12 <?xmlversion= "1.0" encoding= "utf-8" ?> <open-droid> <namevalue= "Demo1" /> <versionvalue= "1" /> <mapping class = "com.example.p033_sqlite.domain.DemoUseBean" /> <!--<mapping class = "com.example.p033_sqlite.domain.DemoYuanBean" />--> <mapping class = "com.example.p033_sqlite.domain.Demo2Bean" /> <namevalue= "Demo2" /> <versionvalue= "1" /> <mapping class = "com.example.p033_sqlite.domain.DemoYuanBean" /> </open-droid> DemoApplication: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 package com.example.p033_sqlite.applications; import android.app.Application; import android.content.Context; import com.example.opendroidlibrary.db.OpenDroidUtil; import com.example.opendroidyuanlibrary.db.OpenDroidYuanUtil; /** *Createdbyshiningon2016/11/100010. */ public class DemoApplication extends Application{ private static final StringTAG= "SFNationApplication" ; private static DemoApplicationsInstance= null ; public static ContextmContext; public static DemoApplicationget(){ if (sInstance== null ){ sInstance= new DemoApplication(); } return sInstance; } @Override public void onCreate(){ super .onCreate(); sInstance= this ; mContext=getApplicationContext(); OpenDroidUtil.setup(mContext); OpenDroidYuanUtil.setup(mContext); } } DemoUseBean: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 package com.example.p033_sqlite.domain; import com.example.opendroidlibrary.db.OpenDroid; /** *Createdbygeekon2016/2/25. */ public class DemoUseBean extends OpenDroid{ private int text_id; private Stringtext_content; public DemoUseBean(){ } public DemoUseBean( int text_id,Stringtext_content){ this .text_id=text_id; this .text_content=text_content; } public int getText_id(){ return text_id; } public void setText_id( int text_id){ this .text_id=text_id; } public StringgetText_content(){ return text_content; } public void setText_content(Stringtext_content){ this .text_content=text_content; } } MainActivityUse: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 package com.example.p033_sqlite.activity; import android.content.ContentValues; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; import com.example.opendroidlibrary.db.OpenDroid; import com.example.p033_sqlite.R; import com.example.p033_sqlite.domain.DemoUseBean; import java.util.List; public class MainActivityUse extends AppCompatActivity implements View.OnClickListener{ private TextViewtv1; private TextViewtv00; private TextViewtv0; private TextViewtv2; private TextViewtv3; private TextViewtv4; @Override protected void onCreate(BundlesavedInstanceState){ super .onCreate(savedInstanceState); setContentView(R.layout.activity_mainuser); tv1=(TextView)findViewById(R.id.tv1); tv00=(TextView)findViewById(R.id.tv00); tv0=(TextView)findViewById(R.id.tv0); tv2=(TextView)findViewById(R.id.tv2); tv3=(TextView)findViewById(R.id.tv3); tv4=(TextView)findViewById(R.id.tv4); tv00.setOnClickListener( this ); tv0.setOnClickListener( this ); tv2.setOnClickListener( this ); tv3.setOnClickListener( this ); tv4.setOnClickListener( this ); } private void get_data(){ Stringa= "" ; List<DemoUseBean>result1=OpenDroid.query.find(DemoUseBean. class ); for (DemoUseBeanres:result1){ System.out.println(res.getText_content()); a=a+res.getText_content()+ "\n\n" ; } tv1.setText(a); } @Override public void onClick(Viewv){ switch (v.getId()){ case R.id.tv00: delete_all_data(); for ( int i= 0 ;i< 10 ;i++){ DemoUseBeanstu= new DemoUseBean(); stu.setText_id(i); stu.setText_content( "opendroid" +i); stu.save(); } get_data(); break ; case R.id.tv0: select_data1(); get_data(); break ; case R.id.tv2: insert_data(); get_data(); break ; case R.id.tv3: update_data(); //update_data_contentvalue(); get_data(); break ; case R.id.tv4: //delete_data(); delete_which_data(); get_data(); break ; default : break ; } } private void update_data_contentvalue(){ /** *step6:使用ContentValues更新 */ ContentValuescv= new ContentValues(); cv.put( "9" , "geekcontentvalue" ); OpenDroid.update(DemoUseBean. class ,cv, "_id=?" , "9" ); DemoUseBeanresult=OpenDroid.query.find(DemoUseBean. class , 9 ); //System.out.println(result.getText_content()); } private void insert_data(){ /** *step1:插入一条数据,查询一条数据 */ DemoUseBeanstu= new DemoUseBean(); stu.setText_content( "geek100" ); stu.setText_id( 100 ); stu.save(); List<DemoUseBean>result=OpenDroid.query.where( "text_id=?" , "100" ).limit( 1 ).find(DemoUseBean. class ); System.out.println(); //result.getText_content() } private void update_data(){ /** *step5:更新数据 */ DemoUseBeanstu= new DemoUseBean(); stu.setText_content( "geek9" ); stu.setText_id( 9 ); stu.update( "text_id=?" , "9" ); List<DemoUseBean>result=OpenDroid.query.find(DemoUseBean. class ); for (DemoUseBeanres:result){ System.out.println(res.getText_content()); } } private void select_data1(){ /** *step2:查询所有记录 */ List<DemoUseBean>result=OpenDroid.query.find(DemoUseBean. class ); for (DemoUseBeanres:result){ System.out.println(res.getText_content()); } } private void select_datas(){ /** *step3:查询多条记录 */ List<DemoUseBean>result=OpenDroid.query.find(DemoUseBean. class , 1 , 5 , 10 ); for (DemoUseBeanres:result){ System.out.println(res.getId()+ ":" +res.getText_content()); } } private void select_which_data(){ /** *step4:条件查询 */ List<DemoUseBean>result=OpenDroid.query.columns( "stuName" , "stuAge" ) .where( "_id>?" , "5" ).order( "_idDESC" ).limit( 3 ) .find(DemoUseBean. class ); for (DemoUseBeanres:result){ System.out.println(res.getText_content()+ ":" +res.getText_id()); } } private void delete_data(){ /** *step7:特定删除 */ int length=OpenDroid.delete(DemoUseBean. class , 9 ); //intlength=OpenDroid.delete(DemoUseBean.class,1,2,3); System.out.println(length); } private void delete_which_data(){ /** *step8:使用条件删除 */ int length=OpenDroid.delete(DemoUseBean. class , "text_id=?" , "9" ); //intlength=OpenDroid.delete(DemoUseBean.class,"_id>?","2"); System.out.println(length); } private void delete_all_data(){ /** *step8:使用条件删除 */ int length=OpenDroid.delete(DemoUseBean. class , "_id>?" , "-1" ); System.out.println(length); } } 总结:这种方式用起来还是很舒服的,源码后面会给出来,你可以外连library,也可以打成aar,都行。 2.greenDao:优点网上大部分都使用,很叼,缺点是每次改变字段,都需要重新自动生成新的greenDao麻蛋真是费劲~ DemoApplication: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 package com.example.p034_greendao_sqlite.application; import android.app.Application; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import com.example.p034_greendao_sqlite.common.DBHelper; import com.example.p034_greendao_sqlite.greendao.DaoMaster; import com.example.p034_greendao_sqlite.greendao.DaoSession; /** *Createdbyshiningon2017/8/29. */ public class DemoApplication extends Application{ private static final StringTAG= "DemoApplication" ; private static DemoApplicationsInstance= null ; public static ContextmContext; private DaoMaster.DevOpenHelpermHelper; private SQLiteDatabasedb; private DaoMastermDaoMaster; private DaoSessionmDaoSession; public static DemoApplicationget(){ if (sInstance== null ){ sInstance= new DemoApplication(); } return sInstance; } @Override public void onCreate(){ super .onCreate(); sInstance= this ; mContext=getApplicationContext(); setDatabase(); } /** *设置greenDao */ private void setDatabase(){ DBHelperdevOpenHelper= new DBHelper( this ); mDaoMaster= new DaoMaster(devOpenHelper.getWritableDb()); mDaoSession=mDaoMaster.newSession(); //userDao=daoSession.getUserDao(); //通过DaoMaster的内部类DevOpenHelper,你可以得到一个便利的SQLiteOpenHelper对象。 //可能你已经注意到了,你并不需要去编写「CREATETABLE」这样的SQL语句,因为greenDAO已经帮你做了。 //注意:默认的DaoMaster.DevOpenHelper会在数据库升级时,删除所有的表,意味着这将导致数据的丢失。 //所以,在正式的项目中,你还应该做一层封装,来实现数据库的安全升级。 //mHelper=newDaoMaster.DevOpenHelper(this,"notes-db",null); //db=mHelper.getWritableDatabase(); ////注意:该数据库连接属于DaoMaster,所以多个Session指的是相同的数据库连接。 //mDaoMaster=newDaoMaster(db); //mDaoSession=mDaoMaster.newSession(); } public DaoSessiongetDaoSession(){ return mDaoSession; } public SQLiteDatabasegetDb(){ return db; } } DBHelper: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 package com.example.p034_greendao_sqlite.common; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import com.example.p034_greendao_sqlite.greendao.DaoMaster; import java.io.IOException; public class DBHelper extends DaoMaster.OpenHelper{ public static final StringDBNAME= "geek2017829133810.db" ; public DBHelper(Contextcontext){ super (context,DBNAME, null ); } @Override public void onUpgrade(SQLiteDatabasedb, int oldVersion, int newVersion){ super .onUpgrade(db,oldVersion,newVersion); //if(oldVersion>newVersion){//TODO存在BUG,卸载后进入ROM自带数据库的时候会出现数据库版本回退会走这段逻辑会出现崩溃 //runCMD("pmclearcom.example.p034_greendao_sqlite"); ////db.setVersion(oldVersion); ////MyLogUtil.d("DBOpenHelper--->onUpgrade-----oldVersion>newVersion"); //this.onCreate(db); //return; //} //if(newVersion==6){//版本2相对于版本1更改了FridgeStatus表,增加了sterilization_mode字段5taginfo增加字段foodStatus ////db.execSQL("droptableifexistsFridgeStatus"); ////db.execSQL("droptableifexistsRFIDDataStatus"); ////db.execSQL("droptableifexiststaginfo"); //} // //this.onCreate(db); } public static StringrunCMD(Stringcmd){ Stringresult= null ; try { String[]cmdx={ "/system/bin/sh" , "-c" ,cmd}; //filemust int ret=ShellExeUtil.execCommand(cmdx); result=String.valueOf(ret); } catch (IOExceptione){ result= "ERR.JE" ; } catch (OutOfMemoryErrore){ System.gc(); System.runFinalization(); result= "ERR.JE" ; } return result; } } MainActivity: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 package com.example.p034_greendao_sqlite; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; import android.widget.Toast; import com.example.p034_greendao_sqlite.application.DemoApplication; import com.example.p034_greendao_sqlite.domain.User; import com.example.p034_greendao_sqlite.greendao.UserDao; import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private TextViewtv0; private TextViewtv_setting; private TextViewtv1; private TextViewtv2; private TextViewtv3; private TextViewtv4; private UserDaomUserDao; @Override protected void onCreate(BundlesavedInstanceState){ super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv0=(TextView)findViewById(R.id.tv0); tv_setting=(TextView)findViewById(R.id.tv_setting); tv1=(TextView)findViewById(R.id.tv1); tv2=(TextView)findViewById(R.id.tv2); tv3=(TextView)findViewById(R.id.tv3); tv4=(TextView)findViewById(R.id.tv4); tv_setting.setOnClickListener( this ); tv1.setOnClickListener( this ); tv2.setOnClickListener( this ); tv3.setOnClickListener( this ); tv4.setOnClickListener( this ); mUserDao=DemoApplication.get().getDaoSession().getUserDao(); } @Override public void onClick(Viewv){ switch (v.getId()){ case R.id.tv_setting: //重置 deleteAllData(); for ( int i= 0 ;i< 10 ;i++){ Userstu= new User(); stu.setId(( long )i); stu.setName( "greendao" +i); stu.setAge(i+ "" ); stu.setSex( "男" +i); stu.setSalary(i+ "" +i); mUserDao.insert(stu); } queryData(); break ; case R.id.tv1: //查询 queryData(); break ; case R.id.tv2: //添加 insertData( 1001 ); queryData(); break ; case R.id.tv3: //更新 updateData( 1001 ); queryData(); break ; case R.id.tv4: //删除 deleteData( 1001 ); queryData(); break ; default : break ; } } //查 private void queryData(){ List<User>users=mUserDao.loadAll(); StringuserName= "" ; for ( int i= 0 ;i<users.size();i++){ userName+=users.get(i).getName()+ "\n\n" ; } tv0.setText(userName); //Toast.makeText(this,"查询全部数据==>"+userName,Toast.LENGTH_SHORT).show(); } //增 private void insertData( int i){ UserinsertData= new User( null ,i+ "" , "geek" , "100" , "男" , "100" ); mUserDao.insert(insertData); } //改 private void updateData( int i){ //UserupdateData=newUser(null,i+"","geek1","101","女","101"); //UserupdateData=newUser((long)i,"geek1","101","女","101"); //mUserDao.update(updateData); Useruser=mUserDao.queryBuilder() .where(UserDao.Properties.Userid.eq(i+ "" ) /*,UserDao.Properties.Name.like("%90%")*/ ).build().unique(); if (user== null ){ Toast.makeText(MainActivity. this , "用户不存在!" ,Toast.LENGTH_SHORT).show(); } else { user.setUserid(i+ "" ); user.setName( "geek1" ); user.setAge( "101" ); user.setSex( "女" ); user.setSalary( "101" ); mUserDao.update(user); } } //删 private void deleteData( int id){ Useruser=mUserDao.queryBuilder().where(UserDao.Properties.Userid.eq(id+ "" )).build().unique(); if (user== null ){ Toast.makeText(MainActivity. this , "用户不存在" ,Toast.LENGTH_SHORT).show(); } else { mUserDao.deleteByKey(user.getId()); } //UserupdateData=newUser((long)i,"geek1","101","女","101"); //mUserDao.deleteByKey((long)id); } //删全部 private void deleteAllData(){ mUserDao.deleteAll(); } } 总结:这种用的很普遍,还是不错的,推荐四颗星~ 3.sugar:优点是轻量级,缺点是Bean的格式转化后要对应规范: DemoBean: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 package com.example.p035_sugar_sqlite.domain; import com.google.gson.annotations.Expose; import com.orm.SugarRecord; import java.io.Serializable; /** *Createdbygeekon2017年8月29日20:44:05. *<p> *Bean的属性名所采用驼峰命名法,那么大写的字母会在创建的表中字段转换成下划线。 *比如spuId这个属性对应的表中的字段名为spu_id *之所以实现Serializable是因为这个Bean在代码中不仅仅为SugarORM创建表而服务, *同时也是为了能在Android组件中传递(比如Handler中的message.obj)而用,所以这里和官网的直接继承自SugarRecord<T>不同,推荐大家用我这种方式 * *@Column(name="textID",unique=true)这个注解意思是说你想强制按照你的规定的名字来创建表中对应的字段名字,所以这里的skuId在Goods表中的字段名就不是默认的sku_id了,而是你自己给的sku_ID *@Expoes是来自于Gson的的一个注解,后面会说到 *@Ignore这个注解强调这个属性在表中不要创建对应的字段 *@SerializedName("id")采用@SerializedName注解,重新指定id的名称 */ public class DemoBean extends SugarRecord implements Serializable{ @Expose private int textid; @Expose private Stringtextcontent1; @Expose private Stringtextcontent2; public DemoBean(){ } public DemoBean( int textid,Stringtextcontent1,Stringtextcontent2){ this .textid=textid; this .textcontent1=textcontent1; this .textcontent2=textcontent2; } public int getTextid(){ return textid; } public void setTextid( int textid){ this .textid=textid; } public StringgetTextcontent1(){ return textcontent1; } public void setTextcontent1(Stringtextcontent1){ this .textcontent1=textcontent1; } public StringgetTextcontent2(){ return textcontent2; } public void setTextcontent2(Stringtextcontent2){ this .textcontent2=textcontent2; } } DemoApplication: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 package com.example.p035_sugar_sqlite.application; import android.app.Application; import android.content.Context; import com.orm.SugarContext; /** *Createdbyshiningon2017/8/29. */ public class DemoApplication extends Application{ private static final StringTAG= "DemoApplication" ; private static DemoApplicationsInstance= null ; public static ContextmContext; public static DemoApplicationget(){ if (sInstance== null ){ sInstance= new DemoApplication(); } return sInstance; } @Override public void onCreate(){ super .onCreate(); sInstance= this ; mContext=getApplicationContext(); SugarContext.init(mContext); } @Override public void onTerminate(){ super .onTerminate(); SugarContext.terminate(); } } MainActivity: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 package com.example.p035_sugar_sqlite; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; import com.example.p035_sugar_sqlite.domain.DemoBean; import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private TextViewtv0; private TextViewtv_setting; private TextViewtv1; private TextViewtv2; private TextViewtv3; private TextViewtv4; @Override protected void onCreate(BundlesavedInstanceState){ super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv0=(TextView)findViewById(R.id.tv0); tv_setting=(TextView)findViewById(R.id.tv_setting); tv1=(TextView)findViewById(R.id.tv1); tv2=(TextView)findViewById(R.id.tv2); tv3=(TextView)findViewById(R.id.tv3); tv4=(TextView)findViewById(R.id.tv4); tv_setting.setOnClickListener( this ); tv1.setOnClickListener( this ); tv2.setOnClickListener( this ); tv3.setOnClickListener( this ); tv4.setOnClickListener( this ); ////find //List<Book>booklist=Book.find(Book.class,"number=?","510110"); //Log.i("sugertest","booklist"+booklist.size()); ////findWithQuery //List<Book>books=Book.findWithQuery(Book.class,"Select*fromBookwherenumber=?","java"); //Log.i("sugertest","books"+books.size()); } @Override public void onClick(Viewv){ switch (v.getId()){ case R.id.tv_setting: //重置 deleteAllData(); for ( int i= 0 ;i< 10 ;i++){ DemoBeanstu= new DemoBean(); //stu.text_id=i; //stu.text_content1="sugar"+i; //stu.text_content2="sugars"+i; stu.setTextid(i); stu.setTextcontent1( "sugar" +i); stu.setTextcontent2( "sugars" +i); stu.save(stu); } select_data(); break ; case R.id.tv1: //查询 select_data(); break ; case R.id.tv2: //添加 add_data(); select_data(); break ; case R.id.tv3: //更新 update_data(); select_data(); break ; case R.id.tv4: //删除 delete_data(); select_data(); break ; default : break ; } } private void select_data(){ //查询一条数据 //List<DemoBean>users=DemoBean.find(DemoBean.class,"text_id>?","-1"); List<DemoBean>users=DemoBean.listAll(DemoBean. class ); StringuserName= "" ; for ( int i= 0 ;i<users.size();i++){ userName+=users.get(i).getId()+ "" + users.get(i).getTextid()+ "" + users.get(i).getTextcontent1()+ "" + users.get(i).getTextcontent2()+ "\n\n" ; } tv0.setText(userName); //DemoBeanqueryBook=DemoBean.findById(DemoBean.class,1); //Log.i("sugertest","queryBook"+queryBook.toString()); } private void add_data(){ //增加一条数据 DemoBeanbook= new DemoBean(); book.setTextid( 101 ); book.setTextcontent1( "geek100" ); book.setTextcontent2( "geek200" ); long rec=book.save(); //Log.i("sugertest","rec"+String.valueOf(rec));//rec=2 } private void update_data(){ //更新一条数据 //List<DemoBean>book1=DemoBean.find(DemoBean.class,"text_id=?","101"); List<DemoBean>book1=DemoBean.findWithQuery(DemoBean. class , "Select*fromDemo_Beanwheretextid=?" , "9" ); book1.get( 0 ).setTextid( 9 ); book1.get( 0 ).setTextcontent1( "geek109" ); book1.get( 0 ).setTextcontent2( "geek109" ); book1.get( 0 ).save(); } private void delete_data(){ //删除一条数据 //DemoBeanbook2=DemoBean.findById(DemoBean.class,101); List<DemoBean>book2=DemoBean.find(DemoBean. class , "textid=?" , "101" ); book2.get( 0 ).delete(); } //删全部 private void deleteAllData(){ DemoBean.deleteAll(DemoBean. class ); } } 总结:还有一种就是直接写SQLite,这里就不做介绍了,大家都写了n遍了吧,不过后面的代码我会附上~relax~ 地址:https://github.com/geeklx/MyApplication p033_sqlite:OpenDroid p034_greendao_sqlite:greenDao p035_sugar_sqlite: sugar 另附图: 本文转自 吴雨声 51CTO博客,原文链接:http://blog.51cto.com/liangxiao/1961376,如需转载请自行联系原作者

优秀的个人博客,低调大师

Android Studio 3.0 最新版本运行报错: Task 'package' is ambiguous in root proje...

09:52:22.876 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 09:52:22.877 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] FAILURE: Build failed with an exception. 09:52:22.877 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 09:52:22.877 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * What went wrong: 09:52:22.877 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Task 'package' is ambiguous in root project 'MyRobot'. Candidates are: 'packageDebug', 'packageDebugAndroidTest', 'packageRelease'. 09:52:22.877 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 09:52:22.877 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * Try: 09:52:22.878 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Run gradle tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. 09:52:22.878 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 09:52:22.878 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * Get more help at https://help.gradle.org 09:52:22.879 [ERROR] [org.gradle.internal.buildevents.BuildResultLogger] 09:52:22.879 [ERROR] [org.gradle.internal.buildevents.BuildResultLogger] BUILD FAILED in 1m 2s 09:52:22.914 [DEBUG] [org.jetbrains.kotlin.gradle.plugin.KotlinGradleBuildServices] [KOTLIN] Skipping kotlin cleanup since compiler wasn't called 09:52:22.944 [DEBUG] [org.jetbrains.kotlin.gradle.plugin.KotlinGradleBuildServices] [KOTLIN] Disposed KotlinGradleBuildServices 09:52:22.945 [DEBUG] [org.gradle.internal.work.DefaultWorkerLeaseService] Worker lease root.1 completed (0 worker(s) in use) $ gradle tasks Task :tasks All tasks runnable from root project Android tasks androidDependencies - Displays the Android dependencies of the project. signingReport - Displays the signing info for each variant. sourceSets - Prints out all the source sets defined in this project. Build tasks assemble - Assembles all variants of all applications and secondary packages. assembleAndroidTest - Assembles all the Test applications. assembleDebug - Assembles all Debug builds. assembleRelease - Assembles all Release builds. build - Assembles and tests this project. buildDependents - Assembles and tests this project and all projects that depend on it. buildNeeded - Assembles and tests this project and all projects it depends on. clean - Deletes the build directory. cleanBuildCache - Deletes the build cache directory. compileDebugAndroidTestSources compileDebugSources compileDebugUnitTestSources compileReleaseSources compileReleaseUnitTestSources mockableAndroidJar - Creates a version of android.jar that's suitable for unit tests. Build Setup tasks init - Initializes a new Gradle build. wrapper - Generates Gradle wrapper files. Help tasks buildEnvironment - Displays all buildscript dependencies declared in root project 'MyRobot'. components - Displays the components produced by root project 'MyRobot'. [incubating] dependencies - Displays all dependencies declared in root project 'MyRobot'. dependencyInsight - Displays the insight into a specific dependency in root project 'MyRobot'. dependentComponents - Displays the dependent components of components in root project 'MyRobot'. [incubating] help - Displays a help message. model - Displays the configuration model of root project 'MyRobot'. [incubating] projects - Displays the sub-projects of root project 'MyRobot'. properties - Displays the properties of root project 'MyRobot'. tasks - Displays the tasks runnable from root project 'MyRobot' (some of the displayed tasks may belong to subprojects). Install tasks installDebug - Installs the Debug build. installDebugAndroidTest - Installs the android (on device) tests for the Debug build. uninstallAll - Uninstall all applications. uninstallDebug - Uninstalls the Debug build. uninstallDebugAndroidTest - Uninstalls the android (on device) tests for the Debug build. uninstallRelease - Uninstalls the Release build. Verification tasks check - Runs all checks. connectedAndroidTest - Installs and runs instrumentation tests for all flavors on connected devices. connectedCheck - Runs all device checks on currently connected devices. connectedDebugAndroidTest - Installs and runs the tests for debug on connected devices. deviceAndroidTest - Installs and runs instrumentation tests using all Device Providers. deviceCheck - Runs all device checks using Device Providers and Test Servers. lint - Runs lint on all variants. lintDebug - Runs lint on the Debug build. lintRelease - Runs lint on the Release build. lintVitalRelease - Runs lint on just the fatal issues in the release build. test - Run unit tests for all variants. testDebugUnitTest - Run unit tests for the debug build. testReleaseUnitTest - Run unit tests for the release build. To see all tasks and more detail, run gradle tasks --all To see more detail about a task, run gradle help --task <task> BUILD SUCCESSFUL in 2s 1 actionable task: 1 executed

优秀的个人博客,低调大师

Android studio 2.2.3分享项目到github教程(纯自己的方法,非Copy)

因为前面看别人的教程之后,各种搞事,----------安装git for windows,各种配置,乱七八糟搞也不知道是不是哪里弄错了还是弄乱了,反正目前百度出来的所有的教程我这边都不能用,出各种问题,最后我在csdn和stackoverflow上都提问了,到现在还没有人回答,不知道是我的问题比较低级还是怎么的 。。http://bbs.csdn.net/topics/392066810这是csdn上的问题,看到的朋友可以帮忙点击下。现在来正式说我的方法:1 首先在github上创建仓库 全部默认即可,可以填写描述之类的2 然后你会得到一个地址 3 然后就去创建工程了,添加各种你的代码,完成之后点击 创建仓库之后会弹出下面的对话框 弹出的时候默认不是选中你的项目的根目录,这边需要选择根目录即可。然后你就会看到你的项目的文件都变成红色的, PS:我这里面有一个.gitignore文件,我知道这是git上传时候忽略不上传的文件,但我也不知道现在每次创建工程项目都带有这个文件了,知道的朋友可以说下然后右击项目,选择git,选择add,文件就会变成绿色的,然后再右击项目,选择git,选择commit Directory 文件变成红色 commit directory 再会弹出commit的对话框,你可以写一些描述 第一次上传 然后点击commit,这里我是没有点击commit and push这个按钮,还是一步步来的好,期间会出现警告之类的,点击确定就可以了。然后再右击项目,选择git,选择仓库,点击push push 我这里弹出一个这样的对话框,我看别人的博客右边应该有东西出现的,可是我这边没有,我也不知道为什么,好像master下面也有东西的,我也是醉了,网上的教程各种乱的,哎! 既然没有东西出现那只能硬来了,我这里点击define remote(这里看的出来可以点击的),这里就用到了再github上创建仓库时的http地址了, name可以改,应该是改成什么都可以吧,我是改成master,下面的URL就填你仓库的地址就可以了然后才会再右边出现你要上传的文件 然后点击Push 等待就可以了,会显示 上传成功 然后刷新github,在你的仓库里面就可以看到你上传的代码了,可以进行一些编辑。 修改描述 然后你在项目中做了一些修改,添加了文件,它会自动提示是否加入git,点击确认就可以了 ,项目修改完之后就右键项目,选择git,选择commit directory又会回到上面上传文件的步骤了,重复步骤上传就可以了。 修改 我的介绍就到这里,欢迎大家指出错误,最后希望大神出一篇完整的教程给新手们用用。

资源下载

更多资源
Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册