ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

<Android考证之实训项目九> SQLit表中图书信息读取

2019-05-31 22:01:45  阅读:194  来源: 互联网

标签:读取 db SQLit DATETIME CONTENT 表中 import android id


  

 1 package com.example.sqlitetest2;
 2 
 3 import android.content.Context;
 4 import android.database.Cursor;
 5 import android.database.sqlite.SQLiteDatabase;
 6 import android.database.sqlite.SQLiteOpenHelper;
 7 import android.support.v7.app.AppCompatActivity;
 8 import android.os.Bundle;
 9 import android.widget.ListView;
10 import android.widget.SimpleCursorAdapter;
11 
12 public class MainActivity extends AppCompatActivity {
13     ListView list;
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.activity_main);
18         list = findViewById(R.id.list);
19 
20         NoteData noteData = new NoteData(this);
21         SQLiteDatabase db = noteData.getReadableDatabase();
22         Cursor cursor = db.rawQuery("select _id,TITLE, substr(CONTENT,1,10)||'...' as CONTENT, DATETIME from notes",null);
23         String[] from = {"_id","TITLE", "CONTENT", "DATETIME"};
24         int[] to = {R.id._id, R.id.title, R.id.content, R.id.datetime};
25         SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this,R.layout.data,cursor,from,to);
26         list.setAdapter(simpleCursorAdapter);
27     }
28 }
29 class NoteData extends SQLiteOpenHelper{
30 
31     private static final String DATABASE_NAME = "test.com.app3.notedata.db";
32     private static final int DATABASE_VERSION = 1;
33     public NoteData(Context context){
34         super(context,DATABASE_NAME,null,DATABASE_VERSION);
35     }
36 
37     @Override
38     public void onCreate(SQLiteDatabase db) {
39         db.execSQL("create table notes(_id integer primary key autoincrement," +
40                 "TITLE text not null," +
41                 "CONTENT text not null," +
42                 "DATETIME text not null);");
43         db.execSQL("insert into notes(TITLE,CONTENT,DATETIME) values('阅读小记','久闻《编程珠玑》一书的大名," +
44                 "一直没有找到合适的机会深入学习阅读,最近终于得以入手,便决心投入细细的研究,提升一下自己的变成思想技术。','2017-03-28')");
45         db.execSQL("insert into notes(TITLE,CONTENT,DATETIME) values('程序调试','程序调试时将编制的程序投入" +
46                 "实际运行前,用手工或编译程序等方法进行测试,修正语法错误和逻辑错误的过程。这是保证计算机信息系统" +
47                 "正确性的必不可少的步骤。','2017-03-28')");
48         db.execSQL("insert into notes(TITLE,CONTENT,DATETIME) values('英语学习','既然想学习英语,那么就对自己狠一点。找一些" +
49                 "高难度的或者全英语的视频电影来看,还有的时可以看英语新闻。','2017-03-31')");
50     }
51 
52     @Override
53     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
54         db.execSQL("drop table if exists notes");
55         onCreate(db);
56     }
57 }
MainActivity.java
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context=".MainActivity">
 8 
 9     <ListView
10         android:id="@+id/list"
11         android:layout_width="match_parent"
12         android:layout_height="match_parent" />
13 </android.support.constraint.ConstraintLayout>
activity_main.xml
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="wrap_content">
 5 
 6     <TextView
 7         android:id="@+id/_id"
 8         android:layout_width="5dp"
 9         android:layout_height="match_parent"
10         android:layout_weight="1"
11         android:gravity="center"
12         android:textSize="18sp"
13         android:text="TextView" />
14 
15     <LinearLayout
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content"
18         android:layout_weight="1"
19         android:orientation="vertical">
20 
21         <TextView
22             android:id="@+id/title"
23             android:layout_width="wrap_content"
24             android:layout_height="wrap_content"
25             android:textSize="18sp"
26             android:text="TextView" />
27 
28         <TextView
29             android:id="@+id/content"
30             android:layout_width="wrap_content"
31             android:layout_height="wrap_content"
32             android:textSize="18sp"
33             android:text="TextView" />
34     </LinearLayout>
35 
36     <TextView
37         android:id="@+id/datetime"
38         android:layout_width="wrap_content"
39         android:layout_height="wrap_content"
40         android:textSize="18sp"
41         android:layout_weight="1"
42         android:text="TextView" />
43 
44 </LinearLayout>
data.xml

 

标签:读取,db,SQLit,DATETIME,CONTENT,表中,import,android,id
来源: https://www.cnblogs.com/jdxb/p/10957746.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有