SQL语句操作SQLite数据库Demo
来源:互联网 发布:ajax解析嵌套json数据 编辑:程序博客网 时间:2024/05/17 07:05
布局文件如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.sqlite.MainActivity" android:orientation="vertical"> <EditText android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/et_desc" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_insert" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="insert"/> <ListView android:id="@+id/lv_info" android:layout_width="match_parent" android:layout_height="wrap_content"/></LinearLayout>Activity代码如下:
public class MainActivity extends AppCompatActivity { private SQLiteDatabase db; private Button btn_insert; private EditText et_name,et_desc; private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // null表示使用默认的工厂 db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+"/my.db3",null); btn_insert = (Button) findViewById(R.id.btn_insert); listView = (ListView) findViewById(R.id.lv_info); et_name = (EditText) findViewById(R.id.et_name); et_desc = (EditText) findViewById(R.id.et_desc); btn_insert.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = et_name.getText().toString(); String desc = et_desc.getText().toString(); try{ insertData(db,name,desc); Cursor cursor = db.rawQuery("select * from news_info",null); inflateCursor(cursor); }catch (SQLiteException e){ db.execSQL("create table news_info(_id integer primary key autoincrement," + " news_name varchar(50),news_desc varchar(255))"); insertData(db,name,desc); Cursor cursor = db.rawQuery("select * from news_info",null); inflateCursor(cursor); } } }); } private void insertData(SQLiteDatabase db,String name,String desc){ db.execSQL("insert into news_info values(null,?,?)", new String[]{name,desc}); } private void inflateCursor(Cursor cursor){ SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.line,cursor, new String[]{"news_name","news_desc"},new int[]{R.id.news_name,R.id.news_desc}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); listView.setAdapter(adapter); } @Override protected void onDestroy() { super.onDestroy(); if(db != null && db.isOpen()){ db.close(); } }}运行效果:
0 0
- SQL语句操作SQLite数据库Demo
- Sqlite数据库操作的sql操作语句
- Sqlite数据库操作的sql操作语句
- 使用SQL语句操作SQLite数据库
- 使用SQL语句操作SQLite数据库
- Android SQLite数据库之一,使用sql语句操作SQLite数据库
- SQLite语句操作数据库
- Android中SQLite数据库操作(1)——使用SQL语句操作SQLite数据库
- SQLite数据库操作--使用SQL语句增删查改
- android对SQLite数据库进行操作(一)SQL语句
- SQL语句 sqlite常见操作
- sqlite数据库中的sql语句
- SQLite数据库中的SQL语句
- SQLite数据库中的SQL语句
- SQLite数据库中的SQL语句
- sqlite数据库中的sql语句
- sqlite数据库中的sql语句
- sqlite数据库中的sql语句
- Android图片质量压缩以及缩略图生成
- Linux 基本指令
- jsp前后台传值那点小事
- TabLayout
- 使用VerticalBannerView实现垂直轮播广告(仿淘宝头条)
- SQL语句操作SQLite数据库Demo
- codeforces基础题——#361(div2)D
- JavaEE实战——jsp入门、El表达式、JSTL标签库
- 在一个千万级的数据库查寻中,如何提高查询效率
- buildroot学习(十)——at91sam9g45软件平台更新
- 百度地图API(一)
- 深入学习absolute
- 第二周项目2 程序的多文件组织
- 【ITOO】AJAX提交Form表单