package com.bw.zgyday8; import androidx.appcompat.app.AppCompatActivity; import android.Manifest; import android.annotation.SuppressLint; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class MainActivity extends AppCompatActivity { NySqilt mySqlite; SQLiteDatabase sqLitDatabase; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mySqlite=new NySqilt(this,"yyw.db",null,1); sqLitDatabase = mySqlite.getWritableDatabase(); if (Build.VERSION.SDK_INT>Build.VERSION_CODES.M) { requestPermissions(new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE },101); } } public void insertdata(View view) { // SqManger.getInstance(this).insert("name","黄狗"); ContentValues contentValues = new ContentValues(); contentValues.put("name","黄狗"); contentValues.put("age","22"); // contentValues.put("name","狗愣"); // contentValues.put("age","26"); sqLitDatabase.insert("stu",null,contentValues); // sqLitDatabase.execSQL("insert into stu values (?,?,?)",new String[]{null,"yyw","28"}); // sqLitDatabase.execSQL("insert into stu values (?,?,?)",new String[]{null,"gmm","18"}); // sqLitDatabase.execSQL("insert into stu values (?,?,?)",new String[]{null,"dhg","48"}); } public void select(View view) { //SqManger.getInstance(this).select("name","黄狗"); //Cursor cursor = sqLitDatabase.rawQuery("select * from stu", null); Cursor cursor = sqLitDatabase.query("stu", null, null, null, null, null, null); while (cursor.moveToNext()) { @SuppressLint("Range") int id=cursor.getInt(cursor.getColumnIndex("id")); @SuppressLint("Range") String name=cursor.getString(cursor.getColumnIndex("name")); @SuppressLint("Range") int age=cursor.getInt(cursor.getColumnIndex("age")); Log.i("---curse","querydata: "+id+name+age); } } public void updatedata(View view) { //SqManger.getInstance(this).update("name","狗愣"); //sqLitDatabase.execSQL("update stu set age=? where name=?",new String[]{"100","gmm"}); ContentValues contentValues = new ContentValues(); contentValues.put("age",100); sqLitDatabase.update("stu",contentValues,"name=?",new String[]{"黄狗"}); } public void seletedata(View view) { //SqManger.getInstance(this).delete("name","狗愣"); //sqLitDatabase.execSQL("delete from stu where name=?",new String[]{"dhg"}); sqLitDatabase.delete("stu","id=?",new String[]{"1"}); } public void traindata(View view) { sqLitDatabase.beginTransaction(); for (int i=0;i<10;i++) { ContentValues contentValues = new ContentValues(); contentValues.put("name","yyw"+i); contentValues.put("age","20"+i); sqLitDatabase.insert("stu",null,contentValues); } sqLitDatabase.setTransactionSuccessful(); sqLitDatabase.endTransaction(); } public void wirtedata(View view) { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File path = Environment.getExternalStorageDirectory(); File file=new File(path,"2101.txt"); try { FileOutputStream fileOutputStream=new FileOutputStream(file); fileOutputStream.write("唱跳rap篮球".getBytes()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } public void readdata(View view) { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File path = Environment.getExternalStorageDirectory(); File file=new File(path,"2101.txt"); try { FileInputStream fileInputStream=new FileInputStream(file); byte[] buffer=new byte[1024]; StringBuilder stringBuilder = new StringBuilder(); int len=0; while ((len=fileInputStream.read(buffer))!=-1) { stringBuilder.append(new String(buffer,0,len)); } String str=stringBuilder.toString(); Log.i("---log", "readdata: "+str); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } public void jpgdata(View view) { new Asyntask().execute("https://cn.bing.com/images/search?view=detailV2&ccid=yB8o04aC&id=C5383F13419917599A77C74BC574FAD5E47891AE&thid=OIP.yB8o04aCwzxjalJTqTkMpwAAAA&mediaurl=https%3a%2f%2fts1.cn.mm.bing.net%2fth%2fid%2fR-C.c81f28d38682c33c636a5253a9390ca7%3frik%3drpF45NX6dMVLxw%26riu%3dhttp%253a%252f%252fqimg.hxnews.com%252f2019%252f0614%252f1560501688399.jpg%26ehk%3dc40lMvGMzZ4LaDGqAj%252bF%252fSMMnnjYmIc7K%252bY3SYC3uHg%253d%26risl%3d%26pid%3dImgRaw%26r%3d0&exph=400&expw=400&q=%e8%94%a1%e5%be%90%e5%9d%a4%e6%89%93%e7%af%ae%e7%90%83&simid=608054661032995080&FORM=IRPRST&ck=E5E7377D2EB7F68D35D908B6827FF507&selectedIndex=6"); } class Asyntask extends AsyncTask<String,Void,String> { @Override protected String doInBackground(String... strings) { try { URL url = new URL(strings[0]); HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection(); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setConnectTimeout(2000); httpURLConnection.setReadTimeout(2000); httpURLConnection.connect(); if (httpURLConnection.getResponseCode()==200) { InputStream inputStream = httpURLConnection.getInputStream(); if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { int len=0; byte[] bytes=new byte[1024]; File path = Environment.getExternalStorageDirectory(); File file = new File(path, "yyw.jpg"); FileOutputStream fileOutputStream=new FileOutputStream(file); while ((len=inputStream.read(bytes))!=-1) { fileOutputStream.write(bytes,0,len); } } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } } }
专高一day9
最新推荐文章于 2024-08-19 17:41:58 发布